• SpringBoot学习7:springboot整合jsp


    springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf,对于java开发人员来说还是大多数人员喜欢使用jsp

    1、创建maven项目,添加pom依赖

    <!--springboot项目依赖的父项目-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
    
        <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,tomcat对jsp的监听 -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
        </dependencies>

    2、创建springboot的全局配置文件application.porerties

    #视图层位置前缀
    spring.mvc.view.prefix=/WEB-INF/jsp/
    #视图层后缀
    spring.mvc.view.suffix=.jsp

    3、创建controller

    package com.bjsxt.controller;
    
    import com.bjsxt.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;
    
    /**
     * Created by Administrator on 2019/2/6.
     */
    @Controller
    public class UserController {
    
        @RequestMapping("/toUserList")
        public String toUserList(Model model){
            List<User> userList=new ArrayList<User>();
            userList.add(new User(1L,"张三","男"));
            userList.add(new User(2L,"李四","女"));
            userList.add(new User(3L,"王五","男"));
            model.addAttribute("userList",userList);
            return "user_list";
        }
    }

    4、创建jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>用户列表</title>
    </head>
    <body>
    
        <table border="1px solid red">
            <tr>
                <th>id</th>
                <th>姓名</th>
                <th>性别</th>
            </tr>
            <c:forEach items="${userList}" var="user">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                    <td>${user.sex}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

    5、创建启动类

    package com.bjsxt;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Created by Administrator on 2019/2/6.
     */
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }

     项目目录结构

  • 相关阅读:
    Web 设计与开发终极资源大全(下)
    任务失败,原因是未找到“LC.exe”,或未安装正确的 Microsoft Windows SDK
    NET开发常用DLL资源下载
    sqlserver2005 sqlserver2000连接字符串的区别(NET)
    创业大讲座观后感
    工欲善其事必先利其器搭建Android平台
    Java学习很好的笔记
    Java调用jama实现矩阵运算
    MySQL的安装及使用教程
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/duanrantao/p/10353812.html
Copyright © 2020-2023  润新知