SpringBoot 集成 JSP
前提:
借助:Maven构建工具
,IntelliJ IDEA
,以及基于SpringBoot 2.3.4
。
可参考SpringBoot官方文档:https://docs.spring.io/spring-boot/docs/
更多干货:
请阅读:《穿越SpringBoot》系列文章
请参考:Java学习资料
背景:
SpringBoot 官方大人对jsp是不建议使用的哦。
按照在下的办法,官人您可以按照下面的步骤执行就可以搞定哟!
目录结构:
来瞧一下,下面这张图:
官人如果不会创建没关系,可以参考:
第一章-SpringBoot 入门 | 第1节- 构建第一个Spring Boot工程创建一个入门先玩玩吧!
使用:
配置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 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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot-web-traditional</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-web-traditional</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- tomcat处理jsp 内置tocat对Jsp支持的依赖,用于编译Jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JstL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<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>
</dependencies>
<build>
<!-- 如果想修改打包的名称,需在build中加入 -->
<finalName>springBootJsp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 下面配置idea主程序不能直接运行主程序的问题 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources/</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
配置application.properties
下图的配置:spring.mvc.view.prefix和spring.mvc.view.suffix是SpringBoot与我们约定的视图前缀后后缀配置,意思是找到/WEB-INF/jsp/目录下的.jsp文件,那么前缀后后缀之间少了文件的名称,这个名称将由控制器(Controller)给出,在控制器中指定返回的路径和名称,就能访问到具体的jsp页面。
#视图解析器的配置
#文件存放的路径
spring.mvc.view.prefix=/WEB-INF/jsp/
#文件后缀
spring.mvc.view.suffix=.jsp
编写BootWebTraditionalApplication 启动类
Spring Boot项目需要
部署在外部容器中的时候,Spring Boot导出的war包如果直接在Tomcat的部署会报错,如需运行需进行如下
修改:
1.启动类继承SpringBootServletInitializer
外部容器部署的话,就不能依赖于Application的main函数
了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,此时我们需要在启动类中继承SpringBootServletInitializer
并实现configure
方法:
2.pom.xml添加spring-boot-starter-tomcat ,上面的pom文件已配置
3.打包方式修改为< packaging>war< /packaging >,上面的pom文件已配置
4.如果想修改打包的名称,需在build节点中加入< finalName>springBootJsp< /finalName>,上面的pom文件已配置
package com.example.bootwebtraditional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.core.SpringVersion;
@SpringBootApplication
public class BootWebTraditionalApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(BootWebTraditionalApplication.class, args);
}
//BootWebTraditionalApplication 需要继承 SpringBootServletInitializer 重写 configure 方法 进行初始化。
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(BootWebTraditionalApplication.class);
}
}
编写DemoController 类
package com.example.bootwebtraditional.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("/demo1")
public String demo1(Model model){
model.addAttribute("data","测试jsp页面的controller");
return "test";
}
}
编写jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
<h1>hello spring boot 中的jsp</h1>
<h2>${data}</h2>
</body>
</html>