SpringBoot使用jsp
SpringBoot使用jsp
·
个人记录
SpringBoot使用jsp
1、新建一个SpringBoot项目
2、新建文件夹webapps,在webapps目录下添加WEB-INF目录
3、File-Project Settings-Models-Web-添加web.xml,没有“Web”选项就自己+一下,注意重新选择目录的话,手动添加上web.xml
然后会自动在webapps/WEB-INF目录下生成 web.xml文件
4、添加当前webapps目录到 Web Resource Directories
△注意不是底下的 Source Roots
5、添加jsp需要的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
然后在 <build></build>
中添加
<resources>
<!--注册Dao包目录下Mybatis映射文件资源目录-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--注册webapp目录为资源目录-->
<!--注意!不用改动下面这几行代码-->
<resource>
<directory>src/main/webapps</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
如下图
6、写代码
在application.properties文件中添加
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
java文件中设置启动
@Controller
public class test {
@RequestMapping("/index")
public String Index(){
return "index";
}
}
webapps目录下新建jsp文件
<%--
Created by IntelliJ IDEA.
User: Lemon
Date: 2022/7/24
Time: 17:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>JSP Page!</p>
</body>
</html>
7、运行
附上所有依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
更多推荐
已为社区贡献5条内容
所有评论(0)