美文网首页
Spring Boot(四)整合视图层技术_JSP

Spring Boot(四)整合视图层技术_JSP

作者: honest涛 | 来源:发表于2020-08-06 14:27 被阅读0次

    作者:毕涛涛,致力于Java学习的践行者。原创文章,转载请注明出处。

    整合JSP

    1.项目整体目录结构: 项目整体目录结构.png

    2.向pom文件中添加坐标

            <!-- 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>
    

    3.创建 springBoot 的全局配置文件,application.properties

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

    4.创建 Controller

    package com.deewinkg.controller;
    
    import com.deewinkg.pojo.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    public class UserController
    {
        /**
         * 处理请求,产生数据。
         * @param model
         * @return
         */
        @RequestMapping("/showUser")
        public String showUser(Model model)
        {
            List<User> userList = new ArrayList<>();
            userList.add(new User("1","刘备",50));
            userList.add(new User("2","关羽",48));
            userList.add(new User("3","张飞",45));
            // 需要一个Model对象
            model.addAttribute("userList",userList);
            // 跳转视图
            return "showUser";
        }
    }
    
    package com.deewinkg.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import lombok.Setter;
    
    import java.io.Serializable;
    @AllArgsConstructor
    @Setter
    @Getter
    public class User implements Serializable
    {
        private String id;
        private String name;
        private int age;
    }
    

    5.创建showUser.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%">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${userList}"  var="user" >
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.name}</td>
                        <td>${user.age}</td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </body>
    </html>
    

    6.创建启动类

    package com.deewinkg;
    
    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);
        }
    }
    
    7.运行结果 运行结果.png

    相关文章

      网友评论

          本文标题:Spring Boot(四)整合视图层技术_JSP

          本文链接:https://www.haomeiwen.com/subject/nvizrktx.html