1 修改pom.xml文件
创建springboot项目加入web模块,并在pom.xml添加如下依赖库
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2 修改项目结构,添加目录
image.png
web.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3 在springboot主配置文件添加jsp映射
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
4 添加控制器
@Controller
public class UserAction {
@RequestMapping("/uli")
public ModelAndView ulist() {
System.out.println("action....");
ModelAndView mv=new ModelAndView("ulist");
List<User> li=new ArrayList<User>();
User u=new User();
u.setUname("享受");
u.setUage(18);
li.add(u);
u=new User();
u.setUname("享受2");
u.setUage(28);
li.add(u);
mv.addObject("tli", li);
return mv;
}
}
5 在webapp目录下创建jsp文件
<%@page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<ul>
<c:forEach var="it" items="${tli }">
<li>${it.uname }</li>
</c:forEach>
</ul>
</body>
</html>
网友评论