o.s.web.servlet.PageNotFound-GET /WEB-INF/views/welcome.jsp没有映射

时间:2018-12-01 08:52:30

标签: java spring-boot

请在拒绝投票之前写下原因,或搁置问题或其他...

我已经创建了一个简单的spring-boot应用程序,但是由于某种原因,该视图无法映射,并且出现以下错误:

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

enter image description here

此语句后出现问题:

return "welcome";

这是 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.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>

我的文件夹结构如下:

enter image description here

这是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";

    }

}

这是application.properties

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

这是DemoApplication类:

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);
    }
}

这是welcome.jsp文件:

<html>
    <head>

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

enter image description here

3 个答案:

答案 0 :(得分:5)

同样的问题也出现在我的身边,并设法通过以下方式解决了这个问题。控制器是相同的,并且初始化初始化Application的方式也相同。

我使用gradle,但是对于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'
}

我使用java config,如下所示:

@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. 您已启用启用配置,否则tomcat无法管理来自WEB-INF的页面
  2. WebConfig应该实现WebMvcConfigurer接口。

答案 1 :(得分:0)

您有一个双重请求映射,一个在类级别,另一个在方法级别,并且两者都相同。请更改方法级别的映射,即将welcome()方法映射到“ /”,说“ hello”,应该可以。

答案 2 :(得分:0)


请参考 SpringBoot - JSP Limitations 部分的官方文档。

enter image description here

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


另一个需要注意的问题:如果使用springboot将项目打包到jar / war文件中,那么用户资源将放置在哪里(例如:用户上传图像或文件)?


您还应该将以下依赖项添加到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>