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

相关推荐
苍何2 小时前
Coding 真有质的飞跃?实测下豆包seed 2.1 pro
后端
苍何2 小时前
试了下腾讯 Marvis,回不去了...
后端
caibixyy2 小时前
springboot+langchain4j 实战 Day14——工具嵌入多 Agent(Tool-Equipped Multi-Agent)
后端
caibixyy2 小时前
springboot+langchain4j 实战 Day13 多 Agent 协作(Router + 子 Agent 分流)
后端
飘尘2 小时前
前端转全栈(Java 后端)必须要知道的:开发中的锁机制与分布式并发控制
前端·后端·全栈
苍何2 小时前
清华团队做了个具身智能大脑,有点东西!
后端
fliter2 小时前
强类型的诅咒,还是 Rust 类型系统的生存指南
后端
用户8356290780513 小时前
Python 操作 PDF 附件:添加、查看与管理指南
后端·python