Spring boot, JSP file as view not loading when run in IntelliJ
Asked Answered
H

13

13

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.

Holcman answered 9/9, 2016 at 9:46 Comment(4)
if my answer is the soution to your problem, then please add the IntelliJ tag to you question and rename it to "Spring boot, JSP file as view not loading in IntelliJ" - that would make the question more helpful for other users.Reputed
/templates != classpath:/templates/ also the JSP shouldn'tb e in static resources they should be in webapp folder (jsp files only work with WAR packaging afaik).Dehumanize
If it's true that jsp files don't work in JAR files then that couldn't possibly be part of the issue, and cause a future issue as I want to package this application as a jar.Holcman
please See this answer IntelliJ IDEA solutionChampaigne
P
17

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

Passably answered 16/4, 2020 at 12:8 Comment(0)
I
13

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.

Icterus answered 20/6, 2018 at 10:9 Comment(0)
P
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

Psychomotor answered 30/11, 2021 at 5:54 Comment(2)
This solution worked for me. I just want to highlight that in my case I was running in a maven multi-module project and the working directory initially set to the parent pom.xml dir. The %MODULOE_WORKING_DIR% will set the main application directory as the working directory.Christchurch
adding WorkingDirectory worked for me.Edea
P
5

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.

Pare answered 11/4, 2017 at 1:6 Comment(0)
R
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/.

Reputed answered 9/9, 2016 at 9:52 Comment(6)
Sadly changing the prefix to the templates directory as suggested didn't work. Same error appearing.Holcman
Have you tryed the "reorder" thing?Reputed
I've not been working with Java and IntelliJ that long, I don't know how to re-order.Holcman
This "hint" is pretty useless. Which prefix? Reorder where?Rocky
I upvoted this because it at least explains the voodoo. At the same time, the link should have better instructions for how to do the two things it suggests (or a link to intellij to give some hints). I still haven't figured out if I tried to the two thing correctly. #moreJavaVoodooSweeper
For future readers, I found this url, but I'm not sure if its a good match for the instructions presented here or not : jetbrains.com/help/idea/working-with-module-dependencies.htmlSweeper
C
2

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

Coating answered 3/10, 2020 at 17:34 Comment(0)
J
1

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

Justiciar answered 18/10, 2017 at 19:18 Comment(1)
why not to use properties?Geanticlinal
P
1

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'

Puppy answered 13/11, 2018 at 6:1 Comment(0)
S
1

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>
Su answered 21/8, 2019 at 12:36 Comment(0)
R
0

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

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

Rianon answered 12/2, 2020 at 16:29 Comment(0)
L
0

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

Leis answered 15/4, 2022 at 17:7 Comment(0)
T
0

if someone is still facing this issue on IntelliJ IDEA then just invalidate cache & restart the IDE. It will clear all downloaded dependencies & re-download all of them again. Hope this should solve your problem.

Telstar answered 23/6, 2022 at 15:51 Comment(0)
M
0

For me it worked after selecting add dependencies with "provided" scope to classpath option.

Version: IntelliJ IDEA 2022.3.3 (Community Edition)

  • Edit Run/Debug Configuration
  • Modify Options
  • Select add dependencies with "provided" scope to classpath
  • Apply
  • Ok
  • Restart application
Mcbrayer answered 24/3, 2023 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.