Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
SheppardDigital Avatar asked Sep 09 '16 09:09

SheppardDigital


People also ask

Where do I put JSP files in Spring Boot IntelliJ?

jsp file in the /src/main/resources/templates/ folder which contains some basic HTML.

Can JSP be used in Spring Boot?

Undertow will not support JSP if used as an Embedded Servlet Container. If deploying in a web container, our @SpringBootApplication annotated class should extend SpringBootServletInitializer and provide necessary configuration options. We can't override the default /error page with JSP.

How do I run Spring Boot jar in IntelliJ?

From the main menu, select Run | Edit Configurations. Select the necessary Spring Boot run configuration to open its settings. Click Modify options. In the list that opens, point to On 'Update' action.

How do I run a JSP file in IntelliJ Community Edition?

3 and all you have to do is go to "Settings > Editor > File Types". Under "Recognized File Types", click on HTML, then under "Registered Patterns" add *. jsp .. Nothing else is required.


2 Answers

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

like image 135
skapral Avatar answered Oct 09 '22 04:10

skapral


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.

like image 28
lazyduckiy Avatar answered Oct 09 '22 02:10

lazyduckiy