springboot项目添加jsp支持

时间:2023-03-08 22:48:33

.创建springboot项目

使用 http://start.spring.io/ 快速创建一个springboot项目下载并导入

springboot项目添加jsp支持

二.添加依赖

在pom.xml中添加支持jsp的依赖如下:

<!-- web  依赖 -->

<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>
<!-- 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>
</dependency>

三.编写controller

@Controller
public class LoginController { @RequestMapping(value="/login")
public String hello(){
return "login";
} }

注意此时注解要用@Controller,不能使用@RestController

四.配置支持jsp的配置文件application.properties

springboot项目添加jsp支持

##jsp  ##
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

五.编写jsp页面

springboot项目添加jsp支持

在项目工程下src/main目录下添加webapp/WEB-INF/jsp文件夹,里面添加jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
Hello!!!
</body>
</html>

六.启动项目启动类访问controller路径

springboot项目添加jsp支持

说明:

1,FreeMarker
2,Groovy
3,Thymeleaf (spring 官网使用这个)
4,Velocity
5,Spring Boot官方不推荐使用JSP ,STS创建的项目会在sec/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如SpringMVC 中的webapp目录)