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自己改變了打包格式引起的。參考之前的文章:http://www.jfrwli.cn/article/161572.html
原來(lái)的結(jié)構(gòu)之下,tomcat是可以?huà)呙璧絝at 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里。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/** * 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的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@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)); } }); } } }; } } |
這樣子的話(huà),spring boot就可以支持fat jar里的jsp資源了。
demo地址:https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample
總結(jié)
-
spring boot改變了打包結(jié)構(gòu),導(dǎo)致tomcat沒(méi)有辦法掃描到fat jar里的/
boot-inf/classes
-
通過(guò)一個(gè)
staticresourceconfigurer
把fat jar里的/boot-inf/classes
加到tomcat的resourceset
來(lái)解決問(wèn)題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/hengyunabc/article/details/80341946