千家信息网

SpringBoot 2.x支持jsp使用jar方式部署吗

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍"SpringBoot 2.x支持jsp使用jar方式部署吗",在日常操作中,相信很多人在SpringBoot 2.x支持jsp使用jar方式部署吗问题上存在疑惑,小编查阅了各式资料,
千家信息网最后更新 2026年11月03日SpringBoot 2.x支持jsp使用jar方式部署吗

这篇文章主要介绍"SpringBoot 2.x支持jsp使用jar方式部署吗",在日常操作中,相信很多人在SpringBoot 2.x支持jsp使用jar方式部署吗问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"SpringBoot 2.x支持jsp使用jar方式部署吗"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一 pom.xml文件修改

1 增加tomcat jsp 依赖

                            javax.servlet            javax.servlet-api            provided                                                   javax.servlet.jsp                   javax.servlet.jsp-api                   2.3.1                                            javax.servlet            jstl                                    org.springframework.boot            spring-boot-starter-tomcat            provided                                                   org.apache.tomcat.embed                    tomcat-embed-jasper                    9.0.27                    provided                

2 修改资源打包配置

                                                           src/main/webapp                    META-INF/resources                                            **/**                                                                        src/main/resources                                            **/**                                                            

二 项目目录

增加目录 src/main/webapp,目录中可加入jsp相关文件

application.properties中加入

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

#支持jsp debugger

server.servlet.jsp.init-parameters.development=true

三 修改代码

增加转换配置

StaticResourceConfigurer.java

package com.vipkid.configuration;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import org.apache.catalina.Context;import org.apache.catalina.Lifecycle;import org.apache.catalina.LifecycleEvent;import org.apache.catalina.LifecycleListener;import org.apache.catalina.WebResourceRoot.ResourceSetType;import org.springframework.util.ResourceUtils;/** * 资源路径转换配置 * Add main class fat jar/exploded directory into tomcat ResourceSet. *  * @author zouqinghua * @date 2019年10月30日  下午9:44:34 * */public class StaticResourceConfigurer implements LifecycleListener {        private final Context context;        StaticResourceConfigurer(Context context) {                this.context = context;        }        @Override        public void lifecycleEvent(LifecycleEvent event) {                if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {                        URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();                        if (ResourceUtils.isFileURL(location)) {                                // when run as exploded directory                                String rootFile = location.getFile();                                if (rootFile.endsWith("/BOOT-INF/classes/")) {                                        rootFile = rootFile.substring(0, rootFile.length() - "/BOOT-INF/classes/".length() + 1);                                }                                if (!new File(rootFile, "META-INF" + File.separator + "resources").isDirectory()) {                                        return;                                }                                try {                                        location = new File(rootFile).toURI().toURL();                                } catch (MalformedURLException e) {                                        throw new IllegalStateException("Can not add tomcat resources", e);                                }                        }                        String locationStr = location.toString();                        if (locationStr.endsWith("/BOOT-INF/classes!/")) {                                // when run as fat jar                                locationStr = locationStr.substring(0, locationStr.length() - "/BOOT-INF/classes!/".length() + 1);                                try {                                        location = new URL(locationStr);                                } catch (MalformedURLException e) {                                        throw new IllegalStateException("Can not add tomcat resources", e);                                }                        }                        this.context.getResources().createWebResourceSet(ResourceSetType.RESOURCE_JAR, "/", location,                                        "/META-INF/resources");                }        }}

增加监听类 TomcatConfiguration.jsp

package com.vipkid.configuration;import org.apache.catalina.Context;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;import org.springframework.boot.web.server.WebServerFactory;import org.springframework.boot.web.server.WebServerFactoryCustomizer;import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import org.springframework.core.annotation.Order;/** * 配置 addLifecycleListener * @author zouqinghua * @date 2019年10月30日  下午9:45:28 * */@Order(Ordered.HIGHEST_PRECEDENCE)@Configuration@ConditionalOnProperty(name = "tomcat.staticResourceCustomizer.enabled", matchIfMissing = true)public class TomcatConfiguration {        /*         *          * SpringBoot 1.x方式配置          @return        @Bean        public EmbeddedServletContainerCustomizer staticResourceCustomizer() {                return new EmbeddedServletContainerCustomizer() {                        @Override                        public void customize(ConfigurableEmbeddedServletContainer container) {                                if (container instanceof TomcatEmbeddedServletContainerFactory) {                                        ((TomcatEmbeddedServletContainerFactory) container)                                                        .addContextCustomizers(new TomcatContextCustomizer() {                                                                @Override                                                                public void customize(Context context) {                                                                        context.addLifecycleListener(new StaticResourceConfigurer(context));                                                                }                                                        });                                }                        }                };        }        */                /**         * SpringBoot 2.x方式配置         * @return         */        @Bean        public WebServerFactoryCustomizer webServerFactoryCustomizerBean(){                                return new WebServerFactoryCustomizer() {                        @Override                        public void customize(ConfigurableTomcatWebServerFactory factory) {                                factory.addContextCustomizers(new TomcatContextCustomizer() {                                                                @Override                                                                public void customize(Context context) {                                                                        context.addLifecycleListener(new StaticResourceConfigurer(context));                                                                }                                                        });                        }                                        };                                                                        }}

四 代码

IndexController和

src/main/webapp/WEB-INF/views/hello.jsp

package com.smc.sys.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController {        @RequestMapping("/hello")    public String index(){                System.out.println("index ==== >>");        return "hello";    }}
<%@ page language="java" contentType="text/html; charset=UTF-8"        pageEncoding="UTF-8"%><%        response.setHeader("Cache-Control", "no-cache");        response.setHeader("Pragma", "no-cache");        response.setHeader("Expires", "0");%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>        

Hello World - ${name}

五 打包 启动

mvn clean package -Dmaven.test.skip=true

java -jar target/xxx.jar

六 访问

http://localhost:8080/hello

到此,关于"SpringBoot 2.x支持jsp使用jar方式部署吗"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

方式 配置 支持 学习 目录 代码 文件 更多 资源 帮助 实用 接下来 文章 方法 理论 知识 篇文章 网站 资料 跟着 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 与互联网科技公司 顺玮阁互联网科技 互联网科技的未来发展 上海亿帐通互联网科技 新华互联网科技校企合作 互联网科技公司招聘 银川新华互联网科技学校地址 常德市电业局互联网科技部 银川新华互联网科技学校联系电话 国大互联网教育科技有限公司薪资 电子商务与互联网科技的关系 广州百诚互联网科技有限公司官网 工业互联网科技成果转化 抖集互联网科技技术咨询 世界互联网科技成果奖 北京天晋互联网科技有限公司 三亚科技创新互联网基地 互联网金融金融科技 北京品多多互联网科技有限公司 互联网高科技绘画 汇久创客互联网科技 广州紫鲸互联网科技 互联网金融和金融科技 互联网教育科技公司vipkid 中国科技创新与世界互联网大会 明优互联网科技有限公司 济南有互联网科技公司吗 互联网硬科技公司 互联网金融运营模式中的金融科技 平安好医生打造互联网科技医疗
0