2

How to configure spring boot through annotations in order to have something similar to in web.xml?

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspf</url-pattern>
        <page-encoding>UTF-8</page-encoding>
        <scripting-invalid>false</scripting-invalid>
        <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
        <default-content-type>text/html</default-content-type>
    </jsp-property-group>
</jsp-config>

3 Answers 3

3

May be it's late but hopefully it will helps other who wants the same thing. I was trying to do the same thing. I started learning spring boot 2 and tried to migrate one of my simple spring mvc application to spring boot. In my spring mvc I was using web.xml and jsp with templating. Although web.xml is very minimal bit it was including the <jsp-config>...</jsp-config> section. So this is what I did in my SpringBoot main file and it is working

@SpringBootApplication
public class SpringBootSimpleApp extends SpringBootServletInitializer  {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootSimpleApp.class);
    }

    /**
     *  <jsp-config>
            <jsp-property-group>
                <url-pattern>*.jsp</url-pattern>
                <url-pattern>*.jspf</url-pattern>
                <page-encoding>UTF-8</page-encoding>

                <!-- This change proves that you have replaced all Java code in your JSPs because any JSPs with Java code cannot compile 
                    with this setting enabled.
                -->
                <scripting-invalid>true</scripting-invalid>
                <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
                <trim-directive-whitespaces>true</trim-directive-whitespaces>
                <default-content-type>text/html</default-content-type>
            </jsp-property-group>
        </jsp-config>
    */
    @Bean 
    public ConfigurableServletWebServerFactory configurableServletWebServerFactory ( ) { 
        return new TomcatServletWebServerFactory() { 
            @Override 
            protected void postProcessContext(Context context) { 
                super.postProcessContext(context); 
                JspPropertyGroup jspPropertyGroup = new JspPropertyGroup(); 
                jspPropertyGroup.addUrlPattern("*.jsp");
                jspPropertyGroup.addUrlPattern("*.jspf");
                jspPropertyGroup.setPageEncoding("UTF-8");
                jspPropertyGroup.setScriptingInvalid("true");
                jspPropertyGroup.addIncludePrelude("/WEB-INF/jsp/base.jspf");
                jspPropertyGroup.setTrimWhitespace("true");
                jspPropertyGroup.setDefaultContentType("text/html"); 
                JspPropertyGroupDescriptorImpl jspPropertyGroupDescriptor = new JspPropertyGroupDescriptorImpl(jspPropertyGroup); 
                context.setJspConfigDescriptor(new JspConfigDescriptorImpl(Collections.singletonList(jspPropertyGroupDescriptor), Collections.emptyList())); 
            } 
        }; 
    }

    public static void main( String[] args ) {
        SpringApplication.run(SpringBootSimpleApp.class, args);
    }

}

Here is my project structure

Spring Boot project structure

Here is my toDos.jsp

<template:main htmlTitle="Landing Page" >
    <div class="container theme-showcase" role="main">
        <div class="jumbotron">
            <h1>ToDo Application</h1>
            <p>A simple Rest API Spring MVC application</p>
        </div>
        <div class="page-header">
            <h1>API</h1>
            <a href="<c:url value = "/services/rest/toDos/get"/>">Current ToDos</a>
        </div>
    </div>
</template:main>

main.tag

<%@ tag body-content="scriptless" trimDirectiveWhitespaces="true" %>
<%@ attribute name="htmlTitle" type="java.lang.String" rtexprvalue="true" required="true" %>
<%@ attribute name="headContent" fragment="true" required="false" %>
<%@ include file="/WEB-INF/jsp/base.jspf" %>
<!DOCTYPE html>
    <head>
        <title>Spring Security &mdash; SAML 2.0 Service Provider :: <c:out value="${fn:trim(htmlTitle)}" /></title>
        <link rel="stylesheet" href="<c:url value="/webjars/bootstrap/4.4.1/css/bootstrap.css" />" />
        <script type="text/javascript" src="<c:url value="/webjars/jquery/3.4.1/jquery.js" />" ></script>
        <script type="text/javascript" src="<c:url value="/webjars/bootstrap/4.4.1/js/bootstrap.js" />" ></script>
        <jsp:invoke fragment="headContent" />
    </head>
    <body>
        <jsp:doBody />
    </body>
</html>

base.jspf

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="template" tagdir="/WEB-INF/tags/template" %>

Hopefully this answer will help others. I was just doing it as I am learning spring boot2.

Thanks

1

I think Dave gave a pretty straight forward answer - that is - there is no Java API defined by the Servlet spec for configuring JSPs and if you must use it with Spring Boot you just need to use web.xml which Spring Boot perfectly supports. This sounds pretty clear to me.

Cheers.

0

I don't think there is a Java API for that stuff. You can still use web.xml with Spring Boot.

6
  • 1
    I think full support for JSP is necessary for the Spring Boot to succeed. If you look at the job trends indeed.com/jobtrends?q=thymeleaf&l= it is flat zero. indeed.com/jobtrends?q=java+jsp%2C+angularjs&l=
    – Bijan
    Commented Jun 19, 2014 at 14:04
  • JSP is dead and there are plenty of alternatives. Spring Boot also supports JSP, so there' no need to give up if that's what you really need to use. We can't change the fact that there is no Java API for configuring though. It's not in the spec.
    – Dave Syer
    Commented Jun 19, 2014 at 14:55
  • 1
    Could you please recommend some practical alternatives on the server side? Thymeleaf is not an alternative because it has not been used widely and it is slow compare to JSP. Even if you look at some companies which provides rich HTML5 components such as Telerik (demos.telerik.com/jsp-ui/line-charts/index) they also provide server side wrapper for their components and for the Java, they just only supports JSP.
    – Bijan
    Commented Jun 19, 2014 at 18:14
  • I think that's beyond the scope of this question (and of stackoverflow generally). The Thymeleaf guys would probably like to know what you mean by "slow" though (it's easy to contact them via github). For my part I'd avoid any 3rd party components that tied me to JSP, but it's totally up to you of course.
    – Dave Syer
    Commented Jun 19, 2014 at 18:53
  • 1
    I guess claiming JSP is dead is not an answer (if we want to follow stackoverflow guidelines). The question was clear and the acceptable answer to the question is how to achieve that in Spring Boot either in a straight manner or through some workaround!
    – Bijan
    Commented Jun 19, 2014 at 19:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.