spring boot 2.x 使用jsp

坑居多

最好不使用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://stackoverflow.com/questions/44689773/why-spring-boot-1-5-3-jar-does-not-recognise-jsp-files-in-src-main-resources-met 

 https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-exploded-war.html

这么古老的东西  慎用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值