'Spring boot: JSP not found (404)

I'm trying to create a new project with spring boot. But I'm getting Error described below. I have added my code.

Error

HTTP ERROR 404 page not found

Here is the link of my project structure. Structure image link

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wc</groupId>
    <artifactId>wc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>wc</name>
    <description>Work configurator project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>`

login.jsp contains just hello

LoginController.class

@Controller
public class LoginController {
@RequestMapping(value = {"/","/login"},method = RequestMethod.GET)
public ModelAndView getLoginPage(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("login");
    return modelAndView;
    }
}

WcApplication.class

@SpringBootApplication
public class WcApplication {

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

Why I'm getting this and how to solve this problem? Thanks



Solution 1:[1]

I think you have missed out ViewResolver configuration class.

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        registry.viewResolver(resolver);
    }
}

Try above configuration and it should call login.jsp page.


Update

As you have correctly added resolver in application.properties. I figured that the problem is with folder WEB-INF. This folder is secured from outer access and .jsp files should not be inside of this folder. It is for files like web.xml which should not be exposed to client.

Just rename it to view (or something else) and change application.properties as -

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

This should definitely work.

Solution 2:[2]

In my case I had everything correctly setup in spring boot. The jsps were in src/main/webapp/WEB-INF/jsp and index.html the welcome page in src/main/resources/static/index.html. Still howevere hard I tried the jsp pages were not getting rendered. I always got 404 return.

But when I changed the build process to create a war field instead of the jar file it worked fine.

Just made additional line change in the build.gradle file as below

apply plugin: "war" 

and after that while ruuing the war as java -jar demo.war everything fell in place and started working.

Solution 3:[3]

I just had this same issue occur. Project structure was correct and everything looked good. I made a slight change to my application.properties spring.mvc.view.prefix. I changed "/WEB-INF/jsp/" to "WEB-INF/jsp/" and everything works correctly. I think in the generate target and war it is the root directory so the additional / as a prefix was confusing something

Solution 4:[4]

Its a hierarchy problem, the WEB-INF should be under webapp folder.

yourMainPackage/src/main/webapp/WEB_INF

(note that for it to work with springboot default configuration, webapp/ should be under main/ and not under resources)

Solution 5:[5]

I faced the same issue and it took me long time to figure out that there are three conditions.

1.) In application properties you need to have the following view resolver mapping (You can use java based as well):

logging.level.web=DEBUG
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp

Note: you have to create "WEB-INF" folder by yourself as in my case "webapp" 
   folder was empty. DEBUG property is not necessary just keep it as it will 
   help you find the root cause from logs. 

2.) I had to make the packaging as "war" instead of jar.

<packaging>war</packaging>

3.) Make sure you have added tomcat jasper dependency to compile the jsp pages at runtime.

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

Hope it helps and feel free to correct me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Aksen P
Solution 3 James Williams
Solution 4 josh
Solution 5 Manish Kumar