您好,登錄后才能下訂單哦!
小編給大家分享一下springboot2.0整合同時(shí)支持jsp+html跳轉(zhuǎn)的案例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
1、pom.xml文件同時(shí)放入thymeleaf 架包和jsp支持后, springboot的return模版會(huì)默認(rèn)跳轉(zhuǎn)到html ,
那怕是你并沒有配置thymeleaf的屬性
解決方案, 使用getRequestDispatcher方法來跳轉(zhuǎn)到j(luò)sp頁面, 就同時(shí)支持html和jsp了
request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);
2、另外 使用getRequestDispatcher跳轉(zhuǎn)到html頁面的時(shí)候,thymeleaf 模版接收參數(shù)可能會(huì)出現(xiàn)問題。
解決方案1:html放棄使用thymeleaf 模版,然后在頁面主動(dòng)請(qǐng)求接口數(shù)據(jù)(AJAX POST等)
解決方案2:html繼續(xù)使用thymeleaf 模版,用return模版 返回來跳轉(zhuǎn)頁面
代碼 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("進(jìn)入了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("進(jìn)入了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("進(jìn)入了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 #非嚴(yán)格模式 prefix: /WEB-INF/ #默認(rèn) classpath:/templates/ suffix: .html servlet: content-type: text/html mybatis: mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #注意:一定要對(duì)應(yīng)mapper映射xml文件的所在路徑 type-aliases-package: com.example.demo.model # 注意:對(duì)應(yīng)實(shí)體類的路徑 configuration: call-setters-on-nulls: true # 解決使用map類型接收查詢結(jié)果的時(shí)候?yàn)閚ull的字段會(huì)沒有的情況
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數(shù)據(jù)庫連接池監(jiān)控依賴 --> <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> <!--非嚴(yán)格模式下 規(guī)避一些html編譯錯(cuò)誤 --> <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標(biāo)簽庫--> <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> <!--解決實(shí)體類啟動(dòng)和jar啟動(dòng)web頁面會(huì)報(bào)404的錯(cuò)誤--> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> </build> </project>
以上就完了。
另外附加一個(gè)return 模版的java代碼配置, 可以配置return模版的優(yōu)先級(jí),后面的文件格式,當(dāng)然只能用getRequestDispatcher來跳轉(zhuǎn)了
在啟動(dòng)類中添加,另外,配置文件參數(shù)和代碼可重復(fù) 但是代碼優(yōu)先于配置文件。
/** * 添加對(duì)jsp支持 * */ @Bean public ViewResolver getJspViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("/WEB-INF/");//前綴 internalResourceViewResolver.setSuffix(".jsp");//后綴 internalResourceViewResolver.setOrder(0);//優(yōu)先級(jí) return internalResourceViewResolver; } /** * 添加對(duì)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);//優(yōu)先級(jí) freeMarkerViewResolver.setContentType("text/html;charset=UTF-8"); return freeMarkerViewResolver; }
看完了這篇文章,相信你對(duì)springboot2.0整合同時(shí)支持jsp+html跳轉(zhuǎn)的案例有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。