2017-08-11 13 views
0

私はSpring MVCを初めて使用しています。しかし、私はHttp Stats 404エラーを受け取りました。私のファイルは以下の通りです:HTTPステータスコード404 spring mvc

これはmavenプロジェクトです。 /Web-Inf/spring-mvc-demo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <!-- Step 3: Add support for component scanning --> 
    <context:component-scan base-package="springdemo" /> 

    <!-- Step 4: Add support for conversion, formatting and validation support --> 
    <mvc:annotation-driven/> 

    <!-- Step 5: Define Spring MVC view resolver --> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

はその後、私はWEB-INF/web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1"> 

    <display-name>spring-mvc-demo</display-name> 

    <!-- Spring MVC Configs --> 

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

/WEB-INF /ビュー/メイン持ちます

<html> 

<body> 

<h1>Welcome to home page!!!!</h1> 

</body> 

</html> 

、その後のsrc/HomeC -page.jsp ontroller.java

package springmvcdemo; 

import org.springframework.stereotype.Component; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Component 
public class HomeController { 

    @RequestMapping("/") 
    public String startPage(){ 
     return "main-page"; 
    } 
} 

は、私は、Eclipse IDEでの動的Webプロジェクトを作成していますし、それは私がそれにTomcat 8.0.44

で実行しようとしていた上でMavenプロジェクトとサーバーで

は誰も私を助けることはでき エラーを排除しますか?

+0

それが正しいとしてみてくださいあなたの 'spring-mvc-demo-servlet.xml'は' web.xml'と同じですか? 'spring-mvc-demo-servlet.xml'を投稿してください。 –

+0

@Rosii Robinson私はファイルを変更しました。間違って同じファイルを作成しました。今それは正しいです。 – Akki

+0

「コンポーネントスキャン」を確認してください。あなたは 'com.luv2code.springdemo'パッケージをスキャンしていて、あなたのコントローラは' springdemo'のように見えます –

答えて

0

サーブレット名はweb.xmlにディスパッチャであるため、春には、ディスパッチャ-servlet.xmlであなたが

+0

のようにコントローラレベルでも '@ RequestMapping'を入れますが、param-initとparam-valueは何を入力しますか?それは正しいXMLを見つけられませんか? – Akki

+0

する必要があります。 Anilが提案したコンポーネントスキャンを追加する –

1

コンポーネント・スキャンを提供しなければならないという名前の設定ファイルディスパッチャ-servlet.xml

をロードしようとする私は、春-MVC-デモ・サーブレットにパッケージ名を参照してください。 XMLを使用すると、コントローラを持っているパッケージと一致していない

春-MVC-デモ-servlet.xml: - 。

にHomeController、

package springmvcdemo; 
0

あなたの例では、それが適切なXSDバージョンを持っていないと原因のjarファイルのバージョンであることができるようになります

よりよいこの例

Spring 4 mvc hello world example

関連する問題