最近在学习SpringBoot,看到SpringBoot整合jsp,顺带记录一下。
1、创建一个SpringBoot项目
点击Next
注意:packaging选中War,点击Next
Webà选中Web,点击Next
输入项目名,点击finish,到此springBoot项目创建完成,
2、项目结构如下:
根据目录创建controller,然后创建JspController类;
main下创建webapp\WEB-INF\jsp\index.jsp
3、Resources文件下,application.properties文件中添加内容:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
|
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|
4、JspController中内容,注解必须为@Controller,return结果为页面名称
package com.springboot.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspController {
@RequestMapping("/index")
public String jspIndex(){
System.out.println("SpringBoot整合jsp。。。");
return "index";
}
}
|
Index,jsp文件中只写了简单的输出
<body>
<h3>SpringBoot整合jsp...</h3>
</body>
|
运行TestApplication,控制台没报错,则启动成功。
5、打开页面,输入地址,跳转到jsp页面
项目module中整合,与创建springboot项目同样的方式创建module,在最外层src\main下创建webapp\WEB-INF\jsp\index.jsp
最终目录结构: