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

相关推荐
凯子坚持 c18 小时前
Docker LXC深度解析:从基础概念到实战演练
java·开发语言
m0_7482336418 小时前
【C++篇】C++11入门:踏入C++新世界的大门
java·c++·算法
Penge66618 小时前
Git LFS 上传大文件避坑指南:从忽略报错到成功推送的完整流程
后端
SWUT胖虎19 小时前
ArkTS 中 @State 底层原理详解
java·list·harmonyos·鸿蒙
SimonKing19 小时前
【开发者必备】Spring Boot 2.7.x:WebMvcConfigurer配置手册来了(四)!
java·后端·程序员
BAGAE19 小时前
HTTPS 加密原理介绍
java·c++·websocket·http·均值算法·启发式算法·最小二乘法
笃行35019 小时前
KingbaseES电科金仓数据库SQL调优
后端
这周也會开心19 小时前
SpringBoot的搭建方式
java·spring boot·后端
varin19 小时前
分析OpenManus源码,建立拥有完全自主规划的AI智能体
后端
Tech有道19 小时前
字节跳动面试:Redis 数据结构有哪些?分别怎么实现的?
后端·面试