【问题标题】:Programmatic Servlet 3.0 JSP jsp-property-group configuration程序化 Servlet 3.0 JSP jsp-property-group 配置
【发布时间】:2015-03-26 21:50:12
【问题描述】:
我能够在我的ServletContainerInitializer 中创建 servlet 和过滤器,但是是否可以将旧的 web.xml 的最后剩余部分转换为 Servlet 3.0 编程配置?
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
【问题讨论】:
标签:
java
jakarta-ee
servlet-3.0
【解决方案1】:
Servlet 3.x 仅为 JSP 设置指定读取接口。
要编写 JSP 设置,需要访问JSP 引擎实现,或者继续使用web.xml。后者不是大问题,因为web.xml 可以与ServletContainerInitializer 安全共存。所以建议保留web.xml。
然而,这是 Spring Boot 的一个问题,它忽略了web.xml。
使用带有嵌入式 Tomcat 的 Spring Boot 2 可以使用 TomcatContextCustomizer 来实现:
@Component
public class JspConfig implements TomcatContextCustomizer {
@Override
public void customize(Context context) {
JspPropertyGroup pg = new JspPropertyGroup();
pg.addUrlPattern("/*");
pg.setPageEncoding("UTF-8");
pg.setTrimWhitespace("true");
ArrayList<JspPropertyGroupDescriptor> pgs = new ArrayList<>();
pgs.add(new JspPropertyGroupDescriptorImpl(pg));
context.setJspConfigDescriptor(new JspConfigDescriptorImpl(pgs, new ArrayList<TaglibDescriptor>()));
}
}