SpringMVC部署到 tomcat 中 web.xml 有一个对 jsp 的配置代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<page-encoding>UTF-8</page-encoding>
<!-- 项目启动时,可以在每个jsp页面上加入 tagelibs.jspf的头文件 -->
<include-prelude>/WEB-INF/tags/taglibs.jspf</include-prelude>
</jsp-property-group>
</jsp-config>
</web-app>
SpringBoot 中项目启动可以使用内嵌的 tomcat 来启动,这时候是没有 web.xml 这个配置文件,但又需要保留对 jsp 的配置,代替的方法:
定制嵌入式容器,在tomcat的容器上下文中我们可以找到Jsp相关的配置类并配置它
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
super.postProcessContext(context);
JspPropertyGroup jspPropertyGroup = new JspPropertyGroup();
jspPropertyGroup.setElIgnored("false");
jspPropertyGroup.addUrlPattern("*.jsp");
jspPropertyGroup.setPageEncoding("UTF-8");
jspPropertyGroup.addIncludePrelude("/WEB-INF/tags/taglibs.jspf");
JspPropertyGroupDescriptorImpl jspPropertyGroupDescriptor =
new JspPropertyGroupDescriptorImpl(jspPropertyGroup);
// jsp-property-group列表和taglib列表
context.setJspConfigDescriptor(
new JspConfigDescriptorImpl(Collections.singletonList(jspPropertyGroupDescriptor),
Collections.emptyList()));
}
};
}
参考:
http://www.ice-maple.com/2019/12/06/springboot%E9%85%8D%E7%BD%AEjsp-config%E7%9A%84%E6%8E%A2%E7%A9%B6/