Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

o.s.web.servlet.PageNotFound - No mapping for GET /WEB-INF/views/welcome.jsp

Please write reasons before down vote or put on hold the question or something else...

I have created simple spring-boot application but for some reason the view can't be mapped and I am getting the following error:

o.s.web.servlet.PageNotFound - No mapping for GET /WEB-INF/views/welcome.jsp

enter image description here

Problem arises after this statement:

return "welcome";

this is pom.xml file:

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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


</project>

My folder structure looks like this:

enter image description here

This is WelcomeController:

package com.example.demo.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class WelcomeController {

    @GetMapping("/")
    public String welcome(){

        return "welcome";

    }

}

This is application.properties:

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
server.port=8089

This is DemoApplication class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

This is welcome.jsp file:

<html>
    <head>

    </head>
    <body>
        this is welcome page
        this is welcome page
    </body>
</html>

enter image description here

like image 859
A.M Avatar asked Dec 01 '18 08:12

A.M


2 Answers

The same issue cropped up at my side as well, and managed to solve it in the following way. The controller is the same, and the same way I initialized the Application as well.

I use gradle, but the dependencies are the same in case of Maven.

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.0.RELEASE'
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.35'
}

I use java config, which looks like:

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
       registry.jsp("/WEB-INF/views/", ".jsp");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
  1. You sould enable configuration otherwise tomcat does not manage to render pages from WEB-INF
  2. WebConfig should implement WebMvcConfigurer interface.
like image 162
Mark Avatar answered Oct 01 '22 22:10

Mark



Please refer to the official doc of SpringBoot - JSP Limitations part.

enter image description here

Github-demo: spring-boot-sample-web-jsp


Another thing to concern: if the project is packaged to a jar/war file using by springboot, where is the user side resources to put (e.g.: user upload an image or a file)?

You should also add the following dependencies to your pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</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>
like image 32
Eddy Avatar answered Sep 30 '22 22:09

Eddy