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插件进行资源拷贝

 

posted @ 2019-08-31 17:18  youngyajun  阅读(1084)  评论(0编辑  收藏  举报