Whitelabel错误页面发生意外错误(类型=未找到,状态= 404). /WEB-INF/views/home.jsp [英] Whitelabel Error Page There was an unexpected error (type=Not Found, status=404). /WEB-INF/views/home.jsp

查看:131
本文介绍了Whitelabel错误页面发生意外错误(类型=未找到,状态= 404). /WEB-INF/views/home.jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

问题是我遇到以下错误:

The problem is that I am getting the following error:

Whitelabel错误页面.

Whitelabel Error Page.

此应用程序没有针对/error的显式映射,因此您将其视为备用.

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

2016年4月4日星期四16:21:23 发生意外错误(类型=未找到,状态= 404). /WEB-INF/views/home.jsp

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

包装结构为:

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

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

DispatcherServlet配置类:

DispatcherServlet Configuration Class:

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类:

WebConfig.java class:

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类: 包com.spittr.config;

RootConfig.java class: package 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 {
}

@Controller类.

The @Controller class.

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:

<?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:

<%@ 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.

谢谢.

推荐答案

问题出在您的项目结构上,WEB-INF应该在src/main/webapp下,而不是在src/main下.

The Problem is with your Project Structure, the WEB-INF should be under src/main/webapp and Not src/main.

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

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

有关 Maven标准目录布局的详细信息.

这是一个 Spring Boot Sample App

PS:如果您打算在Tomcat中部署此应用程序,那么您将面临

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

这篇关于Whitelabel错误页面发生意外错误(类型=未找到,状态= 404). /WEB-INF/views/home.jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆