rus*_*tyx 5 java servlet-3.0 jakarta-ee
我能够在我的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>
Run Code Online (Sandbox Code Playgroud)
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>()));
}
}
Run Code Online (Sandbox Code Playgroud)