雖然說作為視圖技術(shù),JSP不是SpringBoot中提倡的,但是在實(shí)際開發(fā)中很多人還是離不開JSP技術(shù)的,因此在這里我們簡單的整合使用一下。
1.創(chuàng)建項(xiàng)目
2.修改pom文件盟步,添加坐標(biāo)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <groupId>com.neuedu</groupId> <artifactId>08-spring-boot-view-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- SpringBoot啟動器坐標(biāo) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JSTL坐標(biāo) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- jasper坐標(biāo):支持JSP --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!-- 打包的時候,不用把該jar包打包進(jìn)去 --> <scope>provided</scope> </dependency> </dependencies> </project>
3.創(chuàng)建SpringBoot的全局配置文件躏结,application.properties
配置視圖解析器却盘,修改application.properties如下:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
4.創(chuàng)建Controller
package com.neuedu.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.neuedu.po.User; /** * SpringBoot 整合 jsp * @author 清水三千尺 * */ @Controller public class UserController { /* * 處理請求,產(chǎn)生數(shù)據(jù)(模擬數(shù)據(jù)) */ @RequestMapping("/showUser") public String showUser(Model model) { //準(zhǔn)備模擬數(shù)據(jù) List<User> list = new ArrayList<User>(); list.add(new User(1, "趙四", 45)); list.add(new User(2, "劉能", 39)); list.add(new User(3, "廣坤", 43)); //需要一個Model對象 model.addAttribute("list",list); //跳轉(zhuǎn)視圖 return "userList"; } }
5.創(chuàng)建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>ID</th> <th>Name</th> <th>Age</th> </tr> <c:forEach items="${list }" var="user"> <tr> <td>${user.userid }</td> <td>${user.username }</td> <td>${user.userage }</td> </tr> </c:forEach> </table> </body> </html>
6.創(chuàng)建啟動類
package com.neuedu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBoot 啟動類 * @author 清水三千尺 * */ @SpringBootApplication public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
7.測試
訪問路徑:http://localhost:8080/showUser