- 创建springboot工程
- 修改pom.xml文件,添加有关的依赖
<!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
-
创建application.properties文件
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
前缀要一样
-
创建Controller
package com.test.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.test.pojo.User; /** * 整合jsp * @author 26920 * */ @Controller public class UserController { @RequestMapping(value="/user") public String user(Model model){ List<User> list=new ArrayList<User>(); list.add(new User("list",20,"男")); list.add(new User("wanwu",22,"男")); list.add(new User("zhangsan",24,"男")); model.addAttribute("list", list); return "user"; } }
-
创建一个名为user.jsp的文件放到webapp/WEB-INF/jsp下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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> <table border="1" align="center" width="50%"> <tr> <th>name</th> <th>age</th> <th>sex</th> </tr> <c:forEach items="${list }" var="user"> <tr> <th>${user.name }</th> <th>${user.age }</th> <th>${user.sex }</th> </tr> </c:forEach> </table> </body> </html>
-
创建启动类
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { //添加启动方法 SpringApplication.run(App.class, args); } }
-
启动项目访问http://localhost:8080/user就可以看到结果了
springboot整合jspDemo
最新推荐文章于 2023-09-23 07:29:37 发布