JSPサポートのスプリングブート制限
JSPのサポートのために、Spring Bootはパッケージングの戦争方法のみをサポートし、Fat Jarをサポートしていません。公式文書を参照してください:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations
ここでは、Spring Bootは正式にTomcatの問題であると言っていますが、実際にはSpring Boot自体がパッケージング形式を変更することが原因です。以前の記事を参照してください:http://www.vevb.com/article/141479.htm
元の構造の下で、TomcatはFat JarのMETA-INF/resources Directoryの下でリソースをスキャンできます。 BOOT-INF/classesを追加した後、Tomcatはスキャンできません。
では、この問題を解決する方法は? JSP of Spring Boot Fat Jar/Exploded Directoryのサポートを実現するためのソリューションを次に示します。
Tomcatをパーソナライズし、Tomcatのリソースセットにブートインフ/クラスを追加します
Tomcatでは、スキャンされたすべてのリソースがいわゆるResourceSetに配置されます。たとえば、サーブレット3仕様のアプリケーションJARパッケージのMETA-INF/resources ResourceSetです。
次に、 ResourceSetにSpring Bootによって打たれるファットジャーのBOOT-INF/classesディレクトリを追加する方法を見つける必要があります。
次に、TomcatのLifeCyclelistenerインターフェイスを実装することにより、lifecycle.configure_start_eventイベントでBoot-inf/classesのURLを取得し、このURLをWebresourcesetに追加します。
/***メインクラスのファットジャー/爆発ディレクトリをTomcatリソースセットに追加します。 * * @author hengyunabc 2017-07-29 * */public class staticresourceconfigurerは、lifecyclelistener {プライベート最終コンテキスト; staticResourceConfigurer(コンテキストコンテキスト){this.context = context; } @Override public void lifecycleEvent(lifecycleEvent event){if(event.getType()。equals(lifecycle.configure_start_event)){url location = this.getClass()。getProtectionDomain()。 if(resourceutils.isfileurl(location)){//爆発したディレクトリ文字列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){新しいIllegalStateException( "Tomcatリソースを追加できない"、e); }} string locationstr = location.toString(); if(locationStr.Endswith( "/boot-inf/classes!/")){// fat jar locationstr = locationstr.substring(0、locationstr.length() - "/boot-inf/classes!/"。長さ() + 1); try {location = new url(locationStr); } catch(malformedurlexception e){新しいIllegalStateException( "Tomcatリソースを追加できない"、e); }} this.context.getResources()。createewebresourceset(resourcesettype.resource_jar、 "/"、location、 "/meta-inf/resources"); }}}Spring Bootが組み込まれたTomcatを積み込むために、このstateCresourceConfigurerを搭載するには、埋め込まれたservletcontainercustomizer構成も必要です。
@Configuration@ConditionalOnProperty(name = "tomcat.staticResourceCustomizer.enabled", matchIfMissing = true)public class TomcatConfiguration { @Bean public EmbeddedServletContainerCustomizer staticResourceCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public voidカスタマイズ(configureablembeddedservletcontainer container){if(container instance of tomcatembeddedservletcontainerfactory){(((tomcatembeddedservletcontainerfactory)container).addcontextcustomizers(context context context uidize() context.addlifecyclelistener(new staticresourceconfigurer(context)}}); }}}; }}このようにして、Spring BootはFat JarのJSPリソースをサポートできます。
デモアドレス:https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample
要約します
BOOT-INF/classesにスキャンできなくなりましたResourceSet StaticResourceConfigurer Fat Jarのクラスを追加/BOOT-INF/classesを追加して問題を解決します上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。