Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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

like image 837
Ivan Avatar asked May 15 '26 08:05

Ivan


1 Answers

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.

like image 52
Manish Kumar Avatar answered May 17 '26 22:05

Manish Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!