springboot集成jsp

首先pom中引入依赖包

java 复制代码
<!--引入servlet-->
<dependency>
   <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>
<!--引入jstl标签库-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!--增加tomcat处理jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

增加webapp资源打包处理

将webapp下的所有文件打包到target文件下的META-INF/resources下

java 复制代码
<resources>
    <resource>
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
            <include>**/**</include>
        </includes>
    </resource>
</resources>

在main下创建webapp文件夹并进行如下设置File--->Projec Structure中

点击加号选择刚才的webapp文件夹

然后该文件夹就是显示多个点即可

增加配置

java 复制代码
server:
  port: 8081
  servlet:
    context-path: /client
spring:
  mvc:
    view:
      prefix: /WEB-INF/  #View前缀
      suffix: .jsp       #View后缀
    static-path-pattern: /static/**  #访问静态资源时需要加上前缀stati
  resources:
    static-locations: classpath:/META-INF/resources/   #指定静态文件目录

创建jsp和js文件

注意在jsp中增加

java 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

编写controller

java 复制代码
@Slf4j
@Controller
public class IndexController {
    @GetMapping("/v1/index")
    public String loginReq(){
        log.info("执行index");
        return "index";
    }
}

启动

输入地址:http://127.0.0.1:8081/client/static/js/jquery.min.js 查看静态文件

输入地址:http://127.0.0.1:8081/client/v1/index 访问jsp

相关推荐
间彧15 分钟前
Spring Boot集成Spring Security 6.x完整指南
java
绝无仅有32 分钟前
面试实战总结:数据结构与算法面试常见问题解析
后端·面试·github
绝无仅有35 分钟前
Docker 面试常见问题及解答
后端·面试·github
程序员爱钓鱼38 分钟前
Go语言100个实战案例-项目实战篇:股票行情数据爬虫
后端·go·trae
IT_陈寒1 小时前
Redis 性能翻倍的 7 个冷门技巧,第 5 个大多数人都不知道!
前端·人工智能·后端
xiezhr1 小时前
用户只需要知道「怎么办」,不需要知道「为什么炸了」
java·api·接口设计规范
xiezhr1 小时前
接口设计18条军规:写给那些半夜被“502”叫醒的人
java·api·restful
你的人类朋友9 小时前
说说签名与验签
后端
databook9 小时前
Manim实现脉冲闪烁特效
后端·python·动效
RainbowSea10 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程