Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot use resources templates folder with JSP templates instead of webapp folder?

I started a Spring Boot MVC project and realized that there are two folder within resources. One is called templates and the other static. I really like this folder setup.

The problem is that I use JSP Templates for my views. I could not place a .jsp template inside the templates folder and got it to work. What I needed to do is to create a webapp folder on the same level as src and resources. Placing my JSP templates in there and then my views can be found.

What do I need to reconfigure to actually use my JSP templates within the templates folder which lies within resources?

like image 388
xetra11 Avatar asked Sep 12 '17 19:09

xetra11


People also ask

Why does JSP have to be in /WebApp folder?

Somehow, something looks hardwired inside Spring or servlets so that JSP must be in /webapp (or a subfolder). Unlike default thymeleaf templates which are looked up in /resources/templates. I tried all kind of changes, really a lot of different configurations, but wasn't able to modify that behavior.

Where can I put my HTML files in Spring Boot?

By default, Spring Boot looks in my src/main/webapp folder to find my html files. Where can I change the settings for Spring Boot if I use another folder to put the html files? Later, the files will be wrapped in jars for deployment. Are there other things I need to worry about?

Where is the WebApp folder in the spring toolsuite?

On another note, by default, the /webapp folder will also be hidden in the Spring Toolsuite, so you'll have to manually configure it as a "source folder". suppose you have welcome.jsp file in src\main\webapp\WEB-INF\jsp\welcome.jsp and you have these config: spring.mvc.view.prefix: /WEB-INF/jsp/ and spring.mvc.view.suffix: .jsp

What is the difference between JSP and 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.


3 Answers

According to the Maven documentation src/main/resources will end up in WEB-INF/classes in the WAR.

This does the trick for Spring Boot in your application.properties:

spring.mvc.view.prefix = /WEB-INF/classes/templates
spring.mvc.view.suffix = .jsp

If you prefer Java configuration this is the way to go:

@EnableWebMvc
@Configuration
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/classes/templates/");
        bean.setSuffix(".jsp");
        return bean;
    }
}

Update with a full example

This example was based on Spring's initializer (Gradle project with "Web" dependency). I just added apply plugin: 'war' to the build.gradle, added/changed the files below, built teh project with gradle war and deployed it to my application server (Tomcat 8).

This is the directory tree of this sample project:

\---src
    +---main
        +---java
        |   \---com
        |       \---example
        |           \---demo
        |                   ApplicationConfiguration.java
        |                   DemoApplication.java
        |                   DemoController.java
        |
        \---resources
            +---static
            \---templates
                    index.jsp

ApplicationConfiguration.java: see above

DemoApplication.java:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

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

}

DemoController.java:

@Controller
public class DemoController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

index.jsp:

<html>
    <body>
        <h1>Hello World</h1>
    </body>
</html>
like image 119
Franz Fellner Avatar answered Oct 22 '22 20:10

Franz Fellner


Official information:

Resource handling:

Links to resources are rewritten at runtime in template, thanks to a ResourceUrlEncodingFilter, auto-configured for Thymeleaf and FreeMarker. You should manually declare this filter when using JSPs. source

Supported template engine

As well as REST web services, you can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies including Thymeleaf, FreeMarker and JSPs.

[...]

JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers.

[..]

When you’re using one of these templating engines with the default configuration, your templates will be picked up automatically from src/main/resources/templates.

source

Spring boot JSP limitations

  • With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard
    container (not limited to, but including Tomcat).
  • An executable jar will not work because of a hard coded file pattern in Tomcat.
  • With Jetty it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to any standard container.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page won’t override the default view for error handling, custom error pages should be used instead.

source

Technical change

Tell spring boot to from where to load the JSP files. In application.properties set

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

source

Sample spring boot with JSP

In case you do want to use JSP with spring boot here are two examples:

https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp

https://github.com/joakime/spring-boot-jsp-demo

like image 37
oak Avatar answered Oct 22 '22 20:10

oak


To summarize it, none of the suggested answers worked for me so far. Using a blank Spring boot starter project.

Somehow, something looks hardwired inside Spring or servlets so that JSP must be in /webapp (or a subfolder). Unlike default thymeleaf templates which are looked up in /resources/templates.

I tried all kind of changes, really a lot of different configurations, but wasn't able to modify that behavior. It just produced complexity and was unable to serve the JSPs anymore. So, bottom line, if you're using JSPs, just put them in /webapp. It also works by addding zero configuration using a controller like:

@GetMapping("/foo") public String serveFoo() { return "relative-path-inside-webapp/foo.jsp"; }

On another note, by default, the /webapp folder will also be hidden in the Spring Toolsuite, so you'll have to manually configure it as a "source folder".

like image 2
dagnelies Avatar answered Oct 22 '22 22:10

dagnelies