I am able to create servlets and filters in my ServletContainerInitializer, but is it possible to translate this last remaining piece of an old web.xml
into Servlet 3.0 programmatic configuration?
<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>
Servlet 3.x specifies only the reading interface for JSP settings.
To write JSP settings, one need to access the JSP engine implementation, or continue using web.xml
. The latter is not a big issue since web.xml
can safely coexist with ServletContainerInitializer
. So advise is to keep web.xml
.
It is however an issue with Spring Boot, which ignores web.xml
.
With Spring Boot 2 with an embedded Tomcat it can be achieved using 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>()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With