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

相关推荐
缘来是庄19 分钟前
设计模式之访问者模式
java·设计模式·访问者模式
Bug退退退12344 分钟前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
梵高的代码色盘1 小时前
后端树形结构
java
代码的奴隶(艾伦·耶格尔)1 小时前
后端快捷代码
java·开发语言
虾条_花吹雪1 小时前
Chat Model API
java
双力臂4041 小时前
MyBatis动态SQL进阶:复杂查询与性能优化实战
java·sql·性能优化·mybatis
六毛的毛2 小时前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack2 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
31535669132 小时前
一个简单的脚本,让pdf开启夜间模式
前端·后端
程序漫游人2 小时前
centos8.5安装jdk21详细安装教程
java·linux