1.创建maven web project;
2.在添加web依赖
3.配置application.properties支持jsp
4.添加一个controller类
5.加入jsp页面
6.启动类
一、创建maven web project
项目结构
二、添加web依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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> </dependencies>
三、配置application.properties支持jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
四、编写controller类
package com.example; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/index") public String index(Map<String,Object> map){ map.put("name", "HelloController"); return "index"; } }
<html> <body> <h2>Hello ${name} </h2> </body> </html>
六、main启动类
说明:
1,FreeMarker
2,Groovy
3,Thymeleaf (spring 官网使用这个)
4,Velocity
5,Spring Boot官方不推荐使用JSP ,STS创建的项目会在sec/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如SpringMVC 中的webapp目录)