java 白标错误页面 出现意外错误(类型=未找到,状态=404)。/WEB-INF/views/home.jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36482203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:30:57  来源:igfitidea点击:

Whitelabel Error Page There was an unexpected error (type=Not Found, status=404). /WEB-INF/views/home.jsp

javajspspring-mvctomcat

提问by ankel

I am following Spring in Action (part 2) and trying to create the Spittr application as the book shows. (with Spring Tool Suite 7.3.7. and Maven.)

我正在关注 Spring in Action(第 2 部分)并尝试创建本书所示的 Spittr 应用程序。 (with Spring Tool Suite 7.3.7. and Maven.)

The problem is that I am getting the following error:

问题是我收到以下错误:

Whitelabel Error Page.

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Apr 07 16:21:23 CEST 2016 There was an unexpected error (type=Not Found, status=404). /WEB-INF/views/home.jsp

白标错误页面。

此应用程序没有明确的 /error 映射,因此您将其视为后备。

Thu Apr 07 16:21:23 CEST 2016 出现意外错误(类型=未找到,状态=404)。/WEB-INF/views/home.jsp

The package structure is:

包结构为:

image here

图像在这里

As you see I tried to place the /WEB-INF/views/home.jspin several places in, case there was a problem with the path.

如您所见,我尝试将 放在/WEB-INF/views/home.jsp多个位置,以防路径出现问题。

DispatcherServlet Configuration Class:

DispatcherServlet 配置类:

package com.spittr.config;

import  org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
    }
}

WebConfig.java class:

WebConfig.java 类:

package com.spittr.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter
{
    @Bean
    public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");          
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
    DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
    }
}

RootConfig.java class: package com.spittr.config;

RootConfig.java 类:包 com.spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"spitter"},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {
}

The @Controller class.

@Controller 类。

package com.spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController 
{
    @RequestMapping(value="/", method=GET)
    public String home() 
    {
        return "home";
    }
}

pom.xml:

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</groupId>
    <artifactId>spittr</artifactId>
    <version>1.2.0</version>
    <packaging>jar</packaging>

    <name>Spittr</name>
    <description>Test 1</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

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

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

home.jsp:

主页.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Spittr</title>
        <link rel="stylesheet"
        type="text/css"
        href="<c:url value="/resources/style.css" />" >
    </head>
    <body>
        <h1>Welcome to Spittr</h1>
            <a href="<c:url value="/spittles" />">Spittles</a> |
            <a href="<c:url value="/spitter/register" />">Register</a>
    </body>
</html>

Basically is the same that you can find in the book. I simply don't know what else to do.

基本上和你在书中可以找到的一样。我简直不知道还能做什么。

Thanks.

谢谢。

回答by Sanjay Rawat

The Problem is with your Project Structure, the WEB-INFshould be under src/main/webappand Not src/main.

问题在于您的项目结构,WEB-INF应该是 undersrc/main/webapp和 Not src/main

That is, as per your ViewResolveryour JSP file should be under src/main/webapp/WEB-INF/views/home.jsp.

也就是说,根据您ViewResolver的 JSP 文件应该在src/main/webapp/WEB-INF/views/home.jsp.

More info on Maven Standard Directory Layout.

有关Maven 标准目录布局的更多信息。

Here is a Spring Boot Sample App.

这是一个Spring Boot 示例应用程序

PS: If you are planning to deploy this app in Tomcat then you'll face this Issue, the above sample app resolves this issue.

PS:如果您打算在 Tomcat 中部署此应用程序,那么您将面临此问题,上述示例应用程序解决了此问题。

回答by Jagadeesh Rameswarapu

Also you need to make sure to add appropriate dependencies, by adding the eclipse compiler for JDT to pick the jsp file placed in src/main/WEB-INF/*/, and tomcat starter also.

此外,您还需要确保添加适当的依赖项,通过添加 JDT 的 eclipse 编译器来选择放置在 src/main/WEB-INF/*/ 中的 jsp 文件,以及 tomcat starter。

As you already know you need to declare the jsp files location in the application.properties

正如您已经知道的,您需要在 application.properties 中声明 jsp 文件的位置

    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>