Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic Servlet 3.0 JSP jsp-property-group configuration

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>
like image 258
rustyx Avatar asked Nov 09 '22 17:11

rustyx


1 Answers

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>()));
    }
}
like image 174
rustyx Avatar answered Nov 15 '22 11:11

rustyx