Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of web.xml <jsp-config> for Spring Boot MVC?

The JSP specification allows me to serve .html files as JSP (that is, have the container process them as JSP files) using a <jsp-config> section in web.xml, e.g.:

<web-app …>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.html</url-pattern>
    </jsp-property-group>
  </jsp-config>
</web-app>

But when I switch to running a @SpringBootApplication with embedded Tomcat, it completely bypasses the web.xml file. Is there an equivalent setting in Spring Boot MVC to set the JSP configuration of a JSP property group, as per standard web.xml, that will configure the existing embedded Tomcat JSP servlet?

(Another example of a JSP setting I might want to configure is <trim-directive-whitespaces>.)

Before you mark this as a duplicate, please read this closely. I am aware of the extensive answer by walkeros. But that answer only considers adding a new JSP servlet. It does not address adding a new JSP property group to the existing JSP servlet, and indeed doesn't mention the <jsp-config> section in web.xml at all.

like image 314
Garret Wilson Avatar asked May 19 '19 17:05

Garret Wilson


People also ask

Is there web xml in Spring boot?

Application Configuration xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web. xml file. This is no longer the case with Spring Boot.

Does Spring boot support xml configuration?

Spring allows you to configure your beans using Java and XML.

Where is web xml in Spring MVC?

xml under the WebContent/WEB-INF folder.

Can we use JSP with Spring boot?

When building Web Applications, JavaServer Pages (JSP) is one option we can use as a templating mechanism for our HTML pages. On the other hand, Spring Boot is a popular framework we can use to bootstrap our Web Application.


1 Answers

You may find a way to make Spring Boot work with XML configurations, but that is not the way it is supposed to be. To apply special JSP configurations to your application, you could do that as follows:

  • Create a JSP configuration class that implements JspConfigDescriptor , ex:
public class MyJspConfigDescriptor implements JspConfigDescriptor {

    private Collection<JspPropertyGroupDescriptor> jspPropertyGroups =
            new LinkedHashSet<JspPropertyGroupDescriptor>();

    private Collection<TaglibDescriptor> taglibs =
            new HashSet<TaglibDescriptor>();

    @Override
    public Collection<JspPropertyGroupDescriptor> getJspPropertyGroups() {
        JspPropertyGroup newPropertyGroup = new JspPropertyGroup();
        newPropertyGroup.addUrlPattern("*.html");
        // You can add more configurations as you wish!
        JspPropertyGroupDescriptorImpl jspDescriptor = new JspPropertyGroupDescriptorImpl(newPropertyGroup);
        jspPropertyGroups.add(jspDescriptor);
        return jspPropertyGroups;
    }

    @Override
    public Collection<TaglibDescriptor> getTaglibs() {
        return taglibs;
    }

}
  • Inform your SpringBootServletInitializer that there are new configurations that you would like to add, you can do so by overriding the onStartup method and add them there:
@Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.getJspConfigDescriptor().getJspPropertyGroups().addAll((new MyJspConfigDescriptor()).getJspPropertyGroups());
    }

I believe that this would work, but I didn't really test it!

like image 145
Alaa Abuiteiwi Avatar answered Oct 23 '22 11:10

Alaa Abuiteiwi