spring boot 对于jsp支持的限制
对于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官方说是tomcat的问题,实际上是spring boot自己改变了打包格式引起的。参考之前的文章:https://www.zzvips.com/article/161572.html
原来的结构之下,tomcat是可以扫描到fat jar里的meta-inf/resources目录下面的资源的。在增加了boot-inf/classes之后,则tomcat扫描不到了。
那么怎么解决这个问题呢?下面给出一种方案,来实现对spring boot fat jar/exploded directory的jsp的支持。
个性化配置tomcat,把boot-inf/classes 加入tomcat的resourceset
在tomcat里,所有扫描到的资源都会放到所谓的resourceset里。比如servlet 3规范里的应用jar包的meta-inf/resources就是一个resourceset。
现在需要想办法把spring boot打出来的fat jar的boot-inf/classes目录加到resourceset里。
下面通过实现tomcat的 lifecyclelistener接口,在lifecycle.configure_start_event事件里,获取到boot-inf/classes的url,再把这个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加载这个 staticresourceconfigurer,还需要一个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));
}
});
}
}
};
}
}
|
这样子的话,spring boot就可以支持fat jar里的jsp资源了。
demo地址:https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample
总结
-
spring boot改变了打包结构,导致tomcat没有办法扫描到fat jar里的/
boot-inf/classes -
通过一个
staticresourceconfigurer把fat jar里的/boot-inf/classes加到tomcat的resourceset来解决问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/hengyunabc/article/details/80341946
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 80
-
2025-05-29 78
-
MyBatis实现Mysql数据库分库分表操作和总结(推荐)
2025-05-29 50 -
2025-05-25 55
-
2025-05-27 97

