2016-04-23 12 views
0

私はmvcプロジェクトを設定するためにしばらくの間苦労していましたが、私はwelcome.htmlページを何も成功させずに表示しようとしていました。springmvcの表示ページの問題

Below is the structure of the project

上記は、プロジェクトの構造

私にHomeController

 /** 
* 
*/ 
package com.knockknocks.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

    /** 
    * @author vimal 
    * 
    */ 
    @Controller 

    public class HomeController { 
     @RequestMapping(value="/welcome") 
     public ModelAndView home(){ 
      String message = "This is your home"; 
      System.out.println(123456); 
      return new ModelAndView("welcome", "message", message); 
     } 

    } 

作品-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.knockknocks.controller" /> 
<mvc:annotation-driven /> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

web.xmlの

0であります

ポンポンファイル

<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>KnockKnocks</groupId> 
    <artifactId>KnockKnocks</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.5</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.6</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 


    </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.1.3</version> 
     </dependency> 
    </dependencies> 
</project> 

ログイン

Apr 23, 2016 9:23:04 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Apr 23, 2016 9:23:05 PM org.apache.catalina.core.ApplicationContext log 
INFO: No Spring WebApplicationInitializer types detected on classpath 
Apr 23, 2016 9:23:05 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring FrameworkServlet 'works' 
Apr 23, 2016 9:23:05 PM org.springframework.web.servlet.DispatcherServlet initServletBean 
INFO: FrameworkServlet 'works': initialization started 
Apr 23, 2016 9:23:05 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 
INFO: Refreshing WebApplicationContext for namespace 'works-servlet': startup date [Sat Apr 23 21:23:05 MUT 2016]; root of context hierarchy 
Apr 23, 2016 9:23:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/works-servlet.xml] 
Apr 23, 2016 9:23:10 PM org.springframework.web.servlet.DispatcherServlet initServletBean 
INFO: FrameworkServlet 'works': initialization completed in 5153 ms 
Apr 23, 2016 9:23:10 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-nio-8080"] 
Apr 23, 2016 9:23:10 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-nio-8009"] 
Apr 23, 2016 9:23:10 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 12093 ms 

私はURL http://localhost:8080/KnockKnocks/welcome.html accressしようと、私はエラーHTTPステータス404を持っている - /KnockKnocks/welcome.htmlを。

誰かが私を助けてくれることを感謝します。

よろしく

+0

RequestMappingの代わりにvalueを使用してテストします。 –

+0

名前の代わりに値を追加しましたが、私は同じprobを持っています。編集 –

+0

の質問を参照してください。なぜあなたのpom.xmlに "sourceDirectory"がありますか?私はそれが間違っていると言っているわけではありません。目的が何であるか、あなたの問題にそれが関係しているかどうかは不思議です。 –

答えて

2

使用:あなたの中

<mvc:annotation-driven /> 

作品-servlet.xmlは、それはよ

は、コントローラへのリクエストをディスパッチを可能にするために、あなたの春のコンテキストを設定します。

私はこれがあなたを助けてくれることを願っています。

+0

まだ動作しません。上記のコードを更新しました –

2

あなたのサンプルworks.servletショー:

<context:component-scan base-package="com.knockknock.controller" /> 

しかし、あなたにHomeControllerクラスに示されているパッケージです:

package com.knockknocks.controller; 

あなたが一致するように、それらを必要としています。前者は「s」を欠いている。

+0

'web.xml'のバージョンはサーブレットAPIのバージョンであり、Spring Frameworkのバージョンではありません。 – Jesper

+0

ああ、持っています。ありがとう! (その部分の取り外し) –

+0

同じ問題。私はコードを再度更新しました:( –

1

あなたのクラスHomeControllerは、任意のURLをマッピングされていません。

@Controller 
public class HomeController { 

    @RequestMapping(name = "/welcome") 
    public ModelAndView home(){ 
     String message = "This is your home"; 
     return new ModelAndView("welcome", "message", message); 
    } 
} 

あなたは@RequestMapping(value = "/welcome")@RequestMapping(name = "/welcome")を変更する必要があります。
documentが提供したように、@RequestMappingのオプション要素:
name: String - このマッピングに名前を割り当てます。
value: String[] - この注釈によって表現される主マッピング。
...

+0

名前の代わりに値を追加しましたが、同じ問題があります –

+0

STSのSpring TemplateからSpringプロジェクト(HelloWorld)を作成して実行し、プロジェクトを再実行します。私は理由はわかりませんが。 –

1

もう一つのアイデアがあります。 .jspが正しい場所にあり、正しく名前が付けられていることを確認してください。つまり、WEB-INF/jspディレクトリにwelcome.jspがありますか?404ページの特定のメッセージを見て、特定のファイルが見つからないことを確認し、そのファイルがそのディレクトリにあるかどうかを確認してください。

+0

nw –