2016-04-07 5 views
1

私はSpring in Action(パート2)に従っており、本のようにSpittrアプリケーションを作成しようとしています。 (with Spring Tool Suite 7.3.7. and Maven.)ホワイトリストエラーページ予期しないエラーが発生しました(タイプ=見つからない、ステータス= 404)。 /WEB-INF/views/home.jsp

問題は、私は次のエラーを取得していますということです。

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

パッケージ構造は次のとおりです。

image here

あなたは、私はいくつかの場所で/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> 

は基本的に、あなたが本の中で見つけることができるのと同じです。 私は他に何をするか分かりません。

ありがとうございました。

答えて

3

プロジェクトの構造に問題があります。WEB-INFsrc/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に直面します。上記のサンプルアプリケーションはこの問題を解決します。

+0

うんうんうまく働いています。私はあまりにも多くのことを学ぶようだ。ありがとう。 – ankel

関連する問題