This article brings you an introduction to the method of integrating springboot2.0 while supporting jsp html jump (with code). It has certain reference value. Friends in need can refer to it. I hope It will help you.
Explain . Problems encountered during integration,
1. After the pom.xml file is put into the thymeleaf framework package and jsp support at the same time, the return template of springboot will jump to html by default,
even if it is You did not configure the properties of thymeleaf
Solution, use the getRequestDispatcher method to jump to the jsp page, which supports both html and jsp
request.getRequestDispatcher("/WEB-INF/views /testJsp.jsp").forward(request, response);
2. In addition, when using getRequestDispatcher to jump to the html page, there may be problems with the thymeleaf template receiving parameters.
Solution 1: HTML gives up using the thymeleaf template, and then actively requests interface data on the page (AJAX POST, etc.)
Solution 2: HTML continues to use the thymeleaf template, and uses the return template to return to jump Go to the page
代码 UserController.java
package com.example.demo.controller; import com.example.demo.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author chenlin */ @Controller @RequestMapping("/usersDemo") public class UserController { private static Logger log = LoggerFactory.getLogger(UserController.class); @Resource UserService userService; @ResponseBody @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET}) public List
Configuration file
server: port: 8080 tomcat: uri-encoding: UTF-8 servlet: context-path: / spring: dataSource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&usessl=false username: root password: 123456 driverClassName: com.mysql.jdbc.Driver mvc: view: #新版本 1.3后可以使用 suffix: .jsp prefix: /WEB-INF/ view: #老版本 1.4后被抛弃 suffix: .jsp prefix: /WEB-INF/ thymeleaf: #清除缓存 cache: false mode: LEGACYHTML5 #非严格模式 prefix: /WEB-INF/ #默认 classpath:/templates/ suffix: .html servlet: content-type: text/html mybatis: mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径 type-aliases-package: com.example.demo.model # 注意:对应实体类的路径 configuration: call-setters-on-nulls: true # 解决使用map类型接收查询结果的时候为null的字段会没有的情况
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.0.8.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 5.1.47 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 org.springframework.boot spring-boot-starter-test test com.alibaba druid-spring-boot-starter 1.1.13 mysql mysql-connector-java ${mysql.version} org.springframework.boot spring-boot-starter-thymeleaf net.sourceforge.nekohtml nekohtml 1.9.22 org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat.embed tomcat-embed-jasper provided javax.servlet javax.servlet-api 4.0.1 provided javax.servlet jstl src/main/java **/*.xml false src/main/webapp META-INF/resources **/**
The above is done.
In addition, a java code configuration of the return template is attached. You can configure the priority of the return template. Of course, the subsequent file format can only be jumped usinggetRequestDispatcher
Added in the startup class, in addition, the configuration file parameters and code can be repeated but the code takes precedence over the configuration file.
/** * 添加对jsp支持 * */ @Bean public ViewResolver getJspViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("/WEB-INF/");//前缀 internalResourceViewResolver.setSuffix(".jsp");//后缀 internalResourceViewResolver.setOrder(0);//优先级 return internalResourceViewResolver; } /** * 添加对Freemarker支持 * */ @Bean public FreeMarkerViewResolver getFreeMarkerViewResolver() { FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver(); freeMarkerViewResolver.setCache(false); freeMarkerViewResolver.setPrefix("/WEB-INF/");//前缀 freeMarkerViewResolver.setSuffix(".html");//后缀 freeMarkerViewResolver.setRequestContextAttribute("request"); freeMarkerViewResolver.setOrder(1);//优先级 freeMarkerViewResolver.setContentType("text/html;charset=UTF-8"); return freeMarkerViewResolver; }
The above is the detailed content of Introduction to the method of integrating springboot2.0 and supporting jsp+html jump at the same time (with code). For more information, please follow other related articles on the PHP Chinese website!