【问题标题】:Whitelabel Error Page There was an unexpected error (type=Not Found, status=404). /WEB-INF/views/home.jspWhitelabel 错误页面出现意外错误(类型=未找到,状态=404)。 /WEB-INF/views/home.jsp
【发布时间】:2016-07-28 16:34:03
【问题描述】:

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

问题是我收到以下错误:

白标错误页面。

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

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

包结构为:

如你所见,我试图将/WEB-INF/views/home.jsp 放在几个地方,以防路径出现问题。

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

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;

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

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>

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

谢谢。

【问题讨论】:

    标签: java jsp spring-mvc tomcat


    【解决方案1】:

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

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

    更多关于Maven Standard Directory Layout的信息。

    这是Spring Boot Sample App

    PS:如果你打算在 Tomcat 中部署这个应用程序,那么你会遇到这个Issue,上面的示例应用程序解决了这个问题。

    【讨论】:

    • 是的,很好,工作得很好。看来我要学的东西太多了。谢谢。
    • 我的结构是正确的,并且 pom.xml 也是正确的(具有 tomcat 所需的所有依赖项)但仍然没有返回页面。 Controll 正常运行,但未呈现 home.jsp 页面。为什么会这样??
    【解决方案2】:

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

    如您所知,您需要在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      相关资源
      最近更新 更多