osweb.servlet.PageNotFound - 没有映射 GET /WEB-INF/views/welcome.jsp

A.M*_*A.M 6 java spring-boot

请在否决票之前写下原因或搁置问题或其他...

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

o.s.web.servlet.PageNotFound - No mapping for GET /WEB-INF/views/welcome.jsp
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

此语句后出现问题:

return "welcome";
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构如下所示:

在此处输入图片说明

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

    }

}
Run Code Online (Sandbox Code Playgroud)

这是application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
server.port=8089
Run Code Online (Sandbox Code Playgroud)

这是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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是welcome.jsp文件:

<html>
    <head>

    </head>
    <body>
        this is welcome page
        this is welcome page
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Mar*_*ark 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'
}
Run Code Online (Sandbox Code Playgroud)

我使用 java 配置,它看起来像:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 您应该启用配置,否则 tomcat 无法从 WEB-INF
  2. WebConfig应该实现WebMvcConfigurer接口。


Edd*_*ddy 1


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

在此输入图像描述

Github 演示: 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>
Run Code Online (Sandbox Code Playgroud)