9

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>.)

Possible duplicates

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • We can add config file for that – parlad May 28 '19 at 14:58
  • I guess this could help - https://www.baeldung.com/spring-boot-application-configuration – Bilbo Baggins May 29 '19 at 06:40
  • And this too https://stackoverflow.com/questions/44746757/using-custom-tag-files-in-jsp-with-spring-boot ? – Bilbo Baggins May 29 '19 at 06:56
  • 1. you could configure an additional/identical (jstl) viewResolver (for `.html` suffix) [like proposed/described here](https://www.javacodegeeks.com/2016/03/configure-multiple-view-resolvers-spring.html). [A minimal&working spring-boot-jsp setup](https://hellokoding.com/spring-boot-hello-world-example-with-jsp/) ... but the core problem (2.) is: [spring boot is ignoring web.xml](https://stackoverflow.com/q/30910427/592355) / [Spring Boot (doesn't) take over web.xml configuration](https://stackoverflow.com/q/49105350/592355) ..isn't it? – xerx593 May 29 '19 at 08:40
  • ...[Configuring a spring-boot application using web.xml](https://stackoverflow.com/q/19053388/592355) – xerx593 May 29 '19 at 08:44
  • Check out my answer at https://stackoverflow.com/questions/24293901/how-to-configure-spring-boot-through-annotations-in-order-to-have-something-simi/60324780#60324780 – Basit Feb 20 '20 at 16:52

2 Answers2

1

Updated answer:

The spring-boot-starter-web does not rely on web.xml, so you can't use the two in combination. There is a way of using web.xml with Spring Boot, so-called "traditional" way of doing things, exemplified in this sample project. Seems you would just use spring-boot-starter, along with a jsp/web framework (Spring MVC in the sample project), and use web.xml for servlet configuration.

Original answer, pertaining to spring-boot-starter-web (did not work for .html-files):

To configure how Spring boot looks for view-files, use these properties in application.properties:

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .html

To see all properties supported by Spring Boot, see this link. Jump to SPRING MVC to find the ones relevant for this case.

Tobb
  • 11,850
  • 6
  • 52
  • 77
  • So you are saying that if I set `spring.mvc.view.suffix` to `.html`, the embedded servlet container will process my HTML files as JSP? (I seriously doubt that.) Remember we are talking about JSP, which requires coordination with the servlet container, unlike Thymeleaf and other completely custom templating engines. Unless I'm completely off here, I would suggest you read the post by walkeros I linked to in the same question to make sure we're on the same page. I need to pass information to the embedded servlet container. Does Spring (Boot) provide a way to do that? – Garret Wilson May 22 '19 at 16:54
  • I did some fiddling around, and I can't get it to work out of the box. It seems Spring Boot treats files with .html ending as plain static files. Also, it seems it won't serve them from the WEB-INF or META-INF folder, probably for the same reason. In my experience Spring Boot gives a lot of stuff for free, but it requires you to do things the "Spring Boot way" (and in Spring Boot there is no web.xml). Trying to get Spring Boot to do what you want is probably more difficult and will create more trouble than just making the required changes. Could you just rename the files to a `.jsp` ending? – Tobb May 22 '19 at 19:33
  • "Could you just rename the files to a .jsp ending?" But that's not the question, is it? :D And renaming the files doesn't address the other `` issues. Please see the question and research `` in the JSP specification to see what I'm talking about. – Garret Wilson May 23 '19 at 03:23
  • If you want to start using a framework like Spring Boot with the assumption that you don't have to make any changes to your code, you are going to have a bad time ;) Seems you have a couple of choices, either go for Spring Boot the Spring Boot way and forget about web.xml, or stick to web.xml and forget about Spring Boot. In my experience, trying to force a framework to suite ones own personal flavour when it's not the way the framework is made to function is usually a bad idea.. I would assume that Spring Boot is JSP compliant though, and that jsp-config can be implemented in Spring Boot. – Tobb May 23 '19 at 10:28
  • Tobb, your opinion may or may not be correct, but it doesn't answer the question. I downvoted your "answer" because it doesn't answer the question and in fact isn't even correct in relation to the question. Plainly put, your answer is wrong. (Your answer is correct, but for a different question.) – Garret Wilson May 23 '19 at 15:57
  • My answer is not working, that is correct. But it is not supposed to either, from what I've gathered. I have no more control over how Spring Boot works than you, and what you are looking to achieve doesn't seem to be supported. I'll leave my answer undeleted for the discussion in the comments. – Tobb May 23 '19 at 18:39
  • Did one last attempt of finding something, and came across a sample-project that is using Spring Boot along with web.xml. Updated my answer with a link. Might be something worth looking into.. – Tobb May 23 '19 at 18:46
1

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!

  • It is not working I tried. Check this link https://stackoverflow.com/questions/24293901/how-to-configure-spring-boot-through-annotations-in-order-to-have-something-simi/60324780#60324780 – Basit Feb 20 '20 at 16:54