1.新建webapp文件夹
本来我想放在resources/templates目录下面,发现启动后jsp页面找不到,那只能新建webapp文件夹,和resources同一级。
2.idea中设置web项目目录
file-->project structure-->modules-->web +刚才新增的webapp文件夹
3.application.properties设置view的前缀和后缀
#spring mvc配置jsp spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
4.pom.xml添加相关依赖
<!--202201008 add jsp页面使用jstl标签 -->
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--20190927 add 用于编译jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.41</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>9.0.41</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>9.0.41</version>
</dependency>
一般这样就可以了,springboot,我用的是2.7.1的
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent>
如果启动后还访问不了,那么pom.xml build里面加上resources这段试一下
<build>
<!--
SpringBoot项目默认推荐使用的前端引擎是thymeleaf
现在我们要使用SpringBoot集成jsp,手动指定jsp最后编译的路径
同时SpringBoot集成jsp编译jsp的路径是规定好的:META-INF/resources -->
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}${file.separator}classes</targetPath>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<!--这里必须包含.xml否则Mybatis的xml无法打包-->
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<!-- 源文件夹 -->
<directory>src/main/webapp</directory>
<!-- 指定编译到的路径为 META-INF/resources -->
<targetPath>META-INF/resources</targetPath>
<!-- 指定源文件夹中的哪些资源需要进行编译 -->
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--1.4.2.RELEASE打包后jsp页面可以访问,1.4.2以上不支持jsp-->
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
</build>
5.测试页面test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!--引用static中的静态资源-->
<script type="text/javascript" src="static/js/demo.js"></script>
<link type="text/css" rel="stylesheet" href="static/css/demo.css"/>
</head>
<body>
hello,world!hello,china!
<input type="button" onclick="hi()" value="按钮测试"/>
<img src="static/img/java.webp" alt="java">
<div class="fontcss">
你好啊
</div>
</body>
</html>
正常情况下webapp目录下和页面同级的static里面放置js,img,css是可以直接引用的,但是我发现如果搞了一个大的maven project里面再新增module,module里面无法直接访问 ,那么js,css,img就需要放在resources/static这个目录下,然后application.properties需要再配置
#指定编译后classpath所在目录 spring.web.resources.static-locations=classpath:/static/ #指定路径中需要加static才能访问静态资源文件 spring.mvc.static-path-pattern=/static/**
6.访问测试页面
@Controller
public class TestController {
@RequestMapping(value = "/test")
public String test(HttpServletRequest request, HttpServletResponse response) {
return "test";
}
}
http://localhost:8091/study2022/test