Spring Boot新增對jsp的支援
阿新 • • 發佈:2018-11-29
1、在pom.xml新增如下內容:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>
我的pom.xml之前有這麼一段依賴程式碼,需要註釋掉,我試了一下,不註釋也不影響(重複引用?):
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> -->
要注意的是scope需要指定為provided
不新增這些對應的引用會出現各種錯誤,404找不到檔案,或者直接把jsp頁面下載等等
2、在springboot的配置檔案application.properties新增如下內容:
#spring mvc configuration
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
3、編寫測試程式碼
Test2Controller.java
@Controller public class Test2Controller { @GetMapping("test20") public String test20() { return "test20"; } }
test20.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> test20 </body> </html>
我的目錄結構: