天天看點

六、springboot整合jsp1.maven依賴2.修改application.properties3.Controller4.jsp檔案

1.maven依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--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>
           

2.修改application.properties

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

3.Controller

@GetMapping(value = "/showUser")
public String showUser(Model model){
    List<User> list=new ArrayList<>();
    list.add(new User("1","張三",16));
    list.add(new User("2","李四",17));
    list.add(new User("3","王五",18));

    model.addAttribute("list",list);
    return "userList";
}
           

4.jsp檔案

4.1檔案位置

六、springboot整合jsp1.maven依賴2.修改application.properties3.Controller4.jsp檔案

4.2jsp

<%@ 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  align="center" width="50%">
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>Age</th>
        </tr>
        <c:forEach items="${list}" var="user">
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.age}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>
           

繼續閱讀