坑居多
最好不使用jsp 最好不使用jsp 最好不使用jsp
上配置和代码 jsp 在项目中路径为: src/main/webapp/WEB-INF/jsp
application.properties
server.port=8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
启动类
import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@SpringBootApplication
public class TestJspApplication {
public static void main(String[] args) {
SpringApplication.run(TestJspApplication.class, args);
}
@RequestMapping("/")
public String toJps(Model model) {
System.out.println("test-jsp......");
model.addAttribute("msg", "jsp 这种老古董就别用了"+new Date());
return "index";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index.jsp</title>
</head>
<body>
${msg}
</body>
</html>
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.6.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>test-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-jsp</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<!-- 这个版本号是最高支持版本 -->
<version>1.4.2.RELEASE</version>
</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>
可以参考 下面两个网站
https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-exploded-war.html
这么古老的东西 慎用