Spring Boot入门第五天:使用JSP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot入门第五天:使用JSP相关的知识,希望对你有一定的参考价值。

1.在pom.xml文件中添加依赖:

        <!-- Servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</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>

2.配置属性:在application.properties文件添加如下属性:

# JSP页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# JSP响应页面默认后缀
spring.mvc.view.suffix=.jsp

其实完全可以省略这一步骤。

3.编写Controller类:

package com.yws710.springboot.demo1.controller;

import com.yws710.springboot.demo1.domain.User;
import com.yws710.springboot.demo1.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * User控制器类
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/list")
    public ModelAndView userList() throws Exception {
        LOGGER.warn("您执行了UserController类中的userList()方法。");
        ModelAndView mv = new ModelAndView();
        List<User> userList = userService.userList();
        mv.addObject("userList", userList);
        mv.setViewName("/user/list");
        return mv;
    }

}

4.创建jsp文件。WEB-INF/jsp/user/list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <base href="<%=basePath%>">
    <title>用户列表</title>
    <link rel="stylesheet" href="/css/main.css" />
</head>
<body>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>生日</th>
            <th>薪资</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td><fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td>${user.salary}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</body>
</html>

 

5.启动项目,查看运行结果:

技术分享

 

附录:完整pom.xml文件配置

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

    <groupId>com.yws710.springboot</groupId>
    <artifactId>demo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- Servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</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>

        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
        </dependency>
    </dependencies>
</project>

 

以上是关于Spring Boot入门第五天:使用JSP的主要内容,如果未能解决你的问题,请参考以下文章

Python 入门第五天

Python 入门第五天

spring boot入门第一个项目

Android入门第10天-Android访问远程Spring Boot提供的Restful API接口

Python 入门第十五天

Oracle入门第五天(上)——数据库对象之视图