<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<!-- 包含 spring-webmvc、spring-boot-starter、tomcat 嵌入等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSP 編譯引擎 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL 標準標簽庫 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
注意:若不繼承 spring-boot-starter-parent,需按文檔引入 spring-boot-dependencies 管理版本,否則編譯報錯。
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
@SpringBootApplication
@ComponentScan("com.shujushow")
public class Chapter02Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Chapter02Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Chapter02Application.class, args);
}
}
SpringBootServletInitializer 並重寫 configure(),支持打 WAR 部署到外部 Servlet 容器;main() 運行亦可。src/main/webapp/WEB-INF/jsp/ ← 放置 .jsp 文件
Spring Boot 默認不掃描 src/main/webapp,需在 pom.xml 配置 <webappDirectory> 或改用 src/main/resources/META-INF/resources,否則 404。
tomcat-embed-jasper → JSP 無法編譯;jstl → <c:forEach> 等標簽失效;