springboot 2.0 整合 同時支持jsp+html跳轉

springboot項目建立教程 https://blog.csdn.net/q18771811872/article/details/88126835html

springboot2.0 跳轉html教程 https://blog.csdn.net/q18771811872/article/details/88312862java

springboot2.0 跳轉jsp教程 https://blog.csdn.net/q18771811872/article/details/88342298mysql

 說明一下 。整合會遇到的問題,web

一、pom.xml文件同時放入thymeleaf 架包和jsp支持後,  springboot的return模版會默認跳轉到html  ,spring

那怕是你並無配置thymeleaf的屬性sql

解決方案,  使用getRequestDispatcher方法來跳轉到jsp頁面, 就同時支持html和jsp了 數據庫

request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);apache

二、另外 使用getRequestDispatcher跳轉到html頁面的時候,thymeleaf 模版接收參數可能會出現問題。json

解決方案1:html放棄使用thymeleaf 模版,而後在頁面主動請求接口數據(AJAX POST等)api

解決方案2:html繼續使用thymeleaf 模版,用return模版 返回來跳轉頁面

代碼
 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<Map<String, Object>> test(){ log.info("進入了test方法!"); List<Map<String,Object>> list=userService.userQueryAll(); return list; } @RequestMapping(value = "/testHtml", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET}) public String testHtml(HttpServletRequest request, HttpServletResponse response){ List<Map<String,Object>> list=userService.userQueryAll(); request.setAttribute("list",list); log.info("進入了testHtml方法!"); return "views/testHtml"; } @RequestMapping(value = "/testJsp", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET}) public void testJsp( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Map<String,Object>> list=userService.userQueryAll(); request.setAttribute("list",list); log.info("進入了testJsp方法!"); request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response); } } 

配置文件

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

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mysql.version>5.1.47</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- alibaba的druid數據庫鏈接池監控依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.13</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--thymeleaf模版--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--非嚴格模式下 規避一些html編譯錯誤 --> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> <!--tomcat支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!--servlet依賴.--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!--jsp標籤庫--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <resources> <!--解決mybatis文件不編譯問題--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <!--解決實體類啓動和jar啓動web頁面會報404的錯誤--> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> </build> </project> 

以上就完了。  

另外附加一個return 模版的java代碼配置, 能夠配置return模版的優先級,後面的文件格式,固然只能用getRequestDispatcher來跳轉了  

在啓動類中添加,另外,配置文件參數和代碼可重複    可是代碼優先於配置文件。 

/**
* 添加對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;

}

 

 

springboot項目建立教程 https://blog.csdn.net/q18771811872/article/details/88126835

springboot2.0 跳轉html教程 https://blog.csdn.net/q18771811872/article/details/88312862

springboot2.0 跳轉jsp教程 https://blog.csdn.net/q18771811872/article/details/88342298

相關文章
相關標籤/搜索