'Spring boot, JSP file as view not loading when run in IntelliJ

I've created a simple Spring Boot Web Application in intelliJ. I've placed a simple .jsp file in the /src/main/resources/templates/ folder which contains some basic HTML.

I'm trying to return this in a controller but I'm getting this error;

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Sep 09 10:37:46 BST 2016
There was an unexpected error (type=Not Found, status=404).
No message available

I'm assuming that Spring is unable to find the .jsp file, but there's no other errors appearing in the console to give me any further information.

Here's my simple controller;

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("test");
    }

}

I've included the following in my application.properties file;

spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp

My POM file includes the following dependencies;

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

I'm using Spring Boot with embedded tomcat.

I've tried changing the path to the views inside application.properties to; classpath:/templates/ but that also didn't make any difference.

Finally, here is the structure of my project;

Project File Structure

When running the application, I'm just using the 'Run' option in IntelliJ.



Solution 1:[1]

I have recently experienced the same situation with below dependency:-

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

If I change the "scope" to "default", run the application with Intellij, it works fine.

But if I want to keep the "scope" as "provided", I have to use the command line (terminal) and execute the following:-

mvn clean spring-boot:run

It works for me.

Solution 2:[2]

Setting working directory helped me --- by default, it was empty.

  1. Edit your configuration for Spring boot application in Idea.

  2. Set up the working directory, like it's done on a screenshot below:

enter image description here

Solution 3:[3]

If I make the tomcat-embed-jasper dependency default scope (not marked "provided") then everything works ... with spring boot 1.5.2 and idea 2017.1. Otherwise, it's kind of difficult to change this - if you change it in the IDEA project structure, it just gets lost the next time it updates the project from maven or gradle. I haven't figured out a way to otherwise make it work.

Things are further complicated if you use the Spring runner in IDEA -- though I recommend that regardless. It makes things nicer when IDEA fully knows your project is Spring.

Solution 4:[4]

There is a special hint for IntelliJ user in the Spring Boot reference documentation Chapter 27.1.7 Template engines:

IntelliJ IDEA orders the classpath differently depending on how you run your application. Running your application in the IDE via its main method will result in a different ordering to when you run your application using Maven or Gradle or from its packaged jar. This can cause Spring Boot to fail to find the templates on the classpath. If you’re affected by this problem you can reorder the classpath in the IDE to place the module’s classes and resources first. Alternatively, you can configure the template prefix to search every templates directory on the classpath: classpath*:/templates/.

Solution 5:[5]

A bit late but this worked for me. Add your .jsp files at this path:

src/main/resources/META-INF/resources/

And if you want to configure the configure.properties file, add these lines in it:

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

Credit : https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-serve-dynamic.html

Solution 6:[6]

After banging head here and there I figured out how to fix this problem. keep in mind I am using Windows 10 machine and IntelliJ version is 2021.2.2. Here are the steps:

  1. In IntelliJ IDE Click on Run -> Edit Configurations...
  2. In the Working Directory text field put %MODULE_WORKING_DIR%, I guess for linux it may be $MODULE_WORKING_DIR
  3. Click Apply button
  4. Click OK button
  5. Run your Application

It will work. enter image description here

Solution 7:[7]

The following will make the application work with IntelliJ:

Under main, create the folder structure webapp/WEB-INF/jsps/ - the last part of the folder structure can be named anything, but it is where the jsps will reside.

Remove the properties:

spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp

from your application.properties file, and in a Configuration class explicitly create an InternalResourceViewResolver bean, and set these properties there.

This is what your Configuration class will look like:

@Configuration
@EnableWebMvc
public class WebMvcConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsps/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

Give that a try - it should work in IntelliJ

Solution 8:[8]

After many trials, it worked for me. Its the combination of 2 steps.

1) Maven packaging must be set to 'war', then only it puts everything under 'webapp' into the target war file. If packaging is 'jar', its omitting the 'webapp' directory.

2) Running spring boot main class from IntelliJ is not working. Run the application from command prompt using command: 'mvn spring-boot:run'

Solution 9:[9]

In IntelliJ you CAN NOT put provided under tomcat-embed-jasper!

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

Solution 10:[10]

After many trails, I was able to fix the issue:

In IntelliJ 2019.02.04, Spring Boot configuration, select workspace $MODULE_WRK_DIR.

Solution 11:[11]

Controller:

@Controller
public class TestController {

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

Add this to application.properties:

spring.mvc.view.prefix=WEB-INF/

And put your jsp file in src/main/webapp/WEB-INF