springboot整合jsp


1.新建Maven工程

  2.pom關鍵依耐

<?xml version="1.0" encoding="UTF-8"?>
<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>1.5.10.RELEASE</version>
    </parent>
    <groupId>com.cchengyyj</groupId>
    <artifactId>springboot-jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- 修改jdk版本 -->
    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <!-- springBoot的啟動器 -->
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 資源文件拷貝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <!--注意此次必須要放在此目錄下才能被訪問到 -->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
View Code

3.在resources文件夾下創建application.properties或者application.yml,選擇自己喜歡的配置風格,在文件中配置springboot的視圖解析器

application.yml內容如下:

spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
View Code

或者application.properties中添加如下:

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

4.編寫一個Entity(User類),一個Controller(ContentController類)測試

User類:

/**
 * <p> className:    User
 * <p> package:    com.cchengyyj.entity
 * <p> description:    測試Springboot 整合JSP
 * <p> datetime:    2019/8/31   15:59
 * <p> author:   cchengyyj@gmail.com
 */
public class User {

    private int id;
    private String name;
    private String address;

    public User(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
View Code

ContentController類:

/**
 * <p> className:    ContentController
 * <p> package:    com.cchengyyj.controller
 * <p> description: Springboot 整合JSP測試的控制器
 * <p> datetime:    2019/8/31   15:55
 * <p> author:   cchengyyj@gmail.com
 */

@Controller
public class ContentController {
    @GetMapping(value = {"/showUsers"})
    public ModelAndView getUsers(){
        //構建測試數據
        List<User> userList = new ArrayList<User>();
        User u1 = new User(1, "Tom", "America");
        User u2 = new User(2, "LeiLi", "China");
        userList.add(u1);
        userList.add(u2);
        //創建一個模型視圖對象
        ModelAndView modelAndView = new ModelAndView();
        //將數據放置到ModelAndView對象中
        modelAndView.addObject("userList", userList);
        // 指定content.jsp視圖接受model
        modelAndView.setViewName("content");
        //返回ModelAndView對象mav
        return modelAndView;
    }
}
View Code

5.視圖content.jsp獲取數據實現

content.jsp如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Springboot 整合JSP測試</title>
</head>
<body>
    <table border="1" align="center" width="50%">
        <tr>
            <th>ID</th>
            <th>名字</th>
            <th>地址</th>
        </tr>
        <c:forEach var="user" items="${userList}">
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.address}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>
View Code

6.訪問測試

 

 

 注意:當訪問jsp頁面,報404時,去看看編譯出的jar包中是否有jsp頁面,沒有用maven插件進行資源拷貝

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM