怎么在SpringBoot中實(shí)現(xiàn)FatJarjsp的支持

怎么在Spring Boot中實(shí)現(xiàn)Fat Jar jsp的支持?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)昂昂溪,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

spring boot 對(duì)于jsp支持的限制

對(duì)于jsp的支持,Spring Boot官方只支持了war的打包方式,不支持fat jar。參考官方文檔: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations

這里spring boot官方說(shuō)是tomcat的問(wèn)題,實(shí)際上是spring boot自己改變了打包格式引起的。參考之前的文章:https://www.jb51.net/article/141479.htm

原來(lái)的結(jié)構(gòu)之下,tomcat是可以掃描到fat jar里的META-INF/resources目錄下面的資源的。在增加了BOOT-INF/classes之后,則tomcat掃描不到了。

那么怎么解決這個(gè)問(wèn)題呢?下面給出一種方案,來(lái)實(shí)現(xiàn)對(duì)spring boot fat jar/exploded directory的jsp的支持。

個(gè)性化配置tomcat,把BOOT-INF/classes 加入tomcat的ResourceSet

在tomcat里,所有掃描到的資源都會(huì)放到所謂的ResourceSet里。比如servlet 3規(guī)范里的應(yīng)用jar包的META-INF/resources就是一個(gè)ResourceSet。

現(xiàn)在需要想辦法把spring boot打出來(lái)的fat jar的BOOT-INF/classes目錄加到ResourceSet里。

下面通過(guò)實(shí)現(xiàn)tomcat的 LifecycleListener接口,在Lifecycle.CONFIGURE_START_EVENT事件里,獲取到BOOT-INF/classes的URL,再把這個(gè)URL加入到WebResourceSet里。

/**
 * Add main class fat jar/exploded directory into tomcat ResourceSet.
 *
 * @author hengyunabc 2017-07-29
 *
 */
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");

  }
 }
}

為了讓spring boot embedded tomcat加載這個(gè) StaticResourceConfigurer,還需要一個(gè)EmbeddedServletContainerCustomizer的配置:

@Configuration
@ConditionalOnProperty(name = "tomcat.staticResourceCustomizer.enabled", matchIfMissing = true)
public class TomcatConfiguration {
 @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));
        }
       });
    }
   }

  };
 }
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

當(dāng)前標(biāo)題:怎么在SpringBoot中實(shí)現(xiàn)FatJarjsp的支持
當(dāng)前地址:http://muchs.cn/article20/pidcco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)微信公眾號(hào)、微信小程序、網(wǎng)站改版、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)