springboot怎么开发jsp
时间: 2023-06-29 12:02:31 浏览: 102
虽然Spring Boot官方不推荐使用JSP作为视图层技术,但如果你仍然想使用JSP,你可以按照以下步骤进行开发:
1. 在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
```
2. 在application.properties文件中添加以下配置:
```properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
```
3. 在src/main/webapp/WEB-INF目录下创建jsp文件夹,并编写JSP页面。
4. 在Controller中使用`@RequestMapping`注解来映射请求,并返回JSP视图名称。
```java
@Controller
public class UserController {
@RequestMapping("/user")
public String user(Model model) {
User user = new User();
user.setName("Tom");
user.setAge(18);
model.addAttribute(user);
return "user";
}
}
```
5. 在JSP页面中使用JSTL标签来渲染数据。
```jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>User Info</title>
</head>
<body>
<h1>User Info</h1>
<c:out value="${user.name}"/> is <c:out value="${user.age}"/> years old.
</body>
</html>
```
这样就可以使用Spring Boot开发JSP了。但是需要注意的是,由于JSP不是Spring Boot官方推荐的视图层技术,可能会存在一些问题。如果你遇到了问题,建议使用Thymeleaf或者Freemarker等官方推荐的视图层技术。
阅读全文
相关推荐

















