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

相关推荐
Shaoxi Zhang13 分钟前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
赵丙双15 分钟前
CountDownLatch 源码分析
java·aqs·countdownlatch
Python私教34 分钟前
从表格到管理系统:别先写页面,先补齐权限、流程和审计
数据库·后端·架构
余额瞒着我当琳37 分钟前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
thefool1122661 小时前
翻转二叉树
java
北斗落凡尘1 小时前
LangGraph 入门实战(12)--使用MCP
后端·python·langchain
喜欢打篮球的普通人2 小时前
LLVM Backend Lowering 从入门到实战:把 IR 变成机器码的完整链路
android·java·数据库
FL16238631292 小时前
室内易燃物识别易燃评估室内易燃程度识别分割数据集labelme格式1015张85类别
java·服务器·前端
jufeng13072 小时前
【系列:TDengine 工业物联网实战:从零搭起可运行系统 · 第 8 篇】
java·spring boot·时序数据库·tdengine
SomeB1oody3 小时前
【RustyML入门】6.0. 数学工具
开发语言·后端·机器学习·rust·教程