一、环境
Maven 3.6.0
IDEA IntelliJ IDEA 2017.2.6 x64
JDK 1.8
Spring Boot 1.5.4.RELEASE
二、修改pom文件,增加jsp坐标
在springboot项目,springboot官方是不建议使用jsp,因为使用jsp会让springboot很多特性不支持,但是,这里只做介绍,不做深入学习,所以我们需要特别的导入对应的jsp解析器跟对应的jstl标签支持。
<?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.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mqc</groupId>
<artifactId>springboot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-jsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasp</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、创建springboot全局配置文件
下面的修改相当于我们SSM项目中修改对应的视图解析器的前缀跟后缀。
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
五、创建controlelr
package com.mqc.controller;
import com.mqc.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;
/**
* @Auther: maoqichuan
* @Date: 2019/4/9 23:46
* @Description:
*/
@Controller
public class UserController {
@RequestMapping("/userlist")
public String getUserList(Model model){
List<User> list = new ArrayList<>();
list.add(new User("张三","22","001"));
list.add(new User("李四","23","002"));
list.add(new User("王五","25","003"));
model.addAttribute("list",list);
return "userlist";
}
}
六、编写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%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<c:forEach items="${list }" var="user">
<tr>
<td>${user.userid }</td>
<td>${user.username }</td>
<td>${user.userage }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
七、编写启动类
package com.mqc.springbootjsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJspApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJspApplication.class, args);
}
}
启动项目,在浏览器输入:http://localhost:8080/userlist
到这里我们springboot整合jsp已经整合完毕了,有什么问题可以私信我,让我们一起努力,一起进步,加油!!!