SpringBoot中默认是不支持jsp的,但是我们某些情况下可以又需要用到,所以本文记录下SpringBoot整合jsp的过程
整合JSP页面
1.创建控制器
创建一个自定义的控制器,处理请求,并跳转到jsp页面
/** * @program: springboot-02-jsp * @description: SpringBoot整合jsp * @author: 波波烤鸭 * @create: 2019-05-12 21:19 */ @Controller public class UserController { /** * * @param model * @return */ @RequestMapping("/show") public String query(Model model){ model.addAttribute("list",Arrays.asList("张三","李四","王五")); return "user"; } }
2.配置视图解析器前后缀
在application.properties中配置
# 配置视图解析器的前后缀 spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
3.创建jsp文件
在项目的main目录下创建webapp/jsp目录
项目设置web模块,默认没有创建jsp页面的功能,我们需要添加设置
按照如下设置
pom文件修改
<?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 https://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>2.1.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dpb</groupId> <artifactId>springboot-08-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-08-jsp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 导入相关包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>compile</scope> </dependency> <!-- 导入相关包结束 --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> <!-- 为什么要指定目录resources,因为偶尔resource下的properties文件不会打包 为什么目标位置为META-INF/resources,因为这是maven打包之后的标准结构 --> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
创建jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!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> <h1>JSP页面</h1> <c:forEach items="${list }" var="d"> ${d}<br> </c:forEach> </body> </html>
5.启动测试
启动方式注意,与之前有区别,如图:
访问请求: http://localhost:8080/show
搞定~ 整合成功