Spring Boot整合JSP

在Spring Boot应用中,我们有时需要编写JSP页面,如何实现呢?接下来我们看看Spring Boot使用JSP的步骤。

1 创建项目,导入依赖

<?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.yiidian</groupId>
    <artifactId>ch03_03_springboot_jsp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- 导入springboot父工程. 注意:任何的SpringBoot工程都必须有的!!! -->
    <!-- 父工程的作用:锁定起步的依赖的版本号,并没有真正到依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>

    <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- jsp依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>
</project>

这里要记得一定要导入jsp相关的依赖,否则无法运行!

2 配置application.yml

#springmvc视图解析器配置
spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/  # 前缀
      suffix: .jsp  # 后缀

在application.yml我们主要是配置JSP页面访问的前缀和后缀名。

3 编写Controller

UserController:

package com.yiidian.controller;
import java.util.ArrayList;
import java.util.List;
import com.yiidian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 控制器
 * 一点教程网 - www.yiidian.com
 */
@Controller
public class UserController {
	/**
	 * 用户列表展示
	 */
	@RequestMapping("/list")
	public String list(Model model){
		//模拟用户数据
		List<User> list = new ArrayList<User>();
		list.add(new User(1,"小张",18));
		list.add(new User(2,"小李",20));
		list.add(new User(3,"小陈",22));
		
		//把数据存入model
		model.addAttribute("list", list);
		//跳转到jsp页面: list.jsp
		return "list";
	}
}

User:

package com.yiidian.domain;
/**
 * 实体类
 * 一点教程网 - www.yiidian.com
 */
public class User {
	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public User() {
		super();
	}
	public User(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
}

4 编写JSP页面

注意:Spring Boot项目并不会到templates目录查找JSP页面,它是到/src/main/webapp目录下查找。所以我们需要创建webapp目录,然后在里面创建我们需要的目录和JSP文件。

list.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>一点教程网-用户列表展示</title>
</head>
<body>

<h3>用户列表展示</h3>
		<table border="1">
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>年龄</th>
			</tr>
			<c:forEach items="${list}" var="user">
			<tr>
				<td>${user.id}</td>
				<td>${user.name}</td>
				<td>${user.age}</td>
			</tr>
			</c:forEach>
		</table>
</body>
</html>

5 编写引导类

package com.yiidian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * Spring Boot引导类
 * 一点教程网 - www.yiidian.com
 */
@SpringBootApplication
public class MyBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBootApplication.class,args);
    }

}

6 修改项目运行目录

Spring Boot应用默认情况下,会到应用的classes目录下加载内容,因为JSP页面并不在classes下面,所以需要把运行目录手动改为应用的根目录下,这样才能加载到JSP页面。

7 运行测试

http://localhost:8080/list

源码下载:https://pan.baidu.com/s/1B3myrn4QlvfMmVutJkNGpw

热门文章

优秀文章