2016-10-15 12 views
0

私はSpringMVCで新しく、単純なhello worldプログラムを実行しようとしています。しかし、私のブラウザ(http://localhost:8080/FirstspringMVCwithannotation/welcome)でこれを実行すると、HTTPステータス404エラーが出ます。Spring MVCでエラー(HTTPステータス404 - )を取得しています

HelloController.java

package com.example; 

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


@Controller 
public class HelloController { 
    @RequestMapping("/welcome") 
    public ModelAndView helloWorld(){ 
     ModelAndView model=new ModelAndView("HelloPage"); 
     model.addObject("msg","hello world"); 
     return model; 
    } 


} 

web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>FirstspringMVCwithannotation</display-name> 

    <servlet> 
    <servlet-name>spring-dispatcher</servlet-name> 
     <servlet-class> 
        org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>spring-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

春-ディスパッチャ-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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"> 

    <context:component-scan base-package="com.example" /> 
    <mvc:annotation-driven/> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
     <property name="prefix"> 
      <value>/WEB-INF/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
</bean> 
</beans> 

HelloPage.jsp

:ここでは、コードがあります
<html> 
    <body> 
     <h1> 
      First Spring MVC Application Demo 
     </h1> 
     <h2> 
      ${msg} 
     </h2> 
    </body> 
</html> 
ここ

私のプロジェクト構造であると私は他のソリューションを参照しようとしたが、それは私のproblem..can誰も私を助けてください解決しない、なぜ私のlibフォルダの下に

enter image description here

をすべて春のjarファイルを追加しましたHTTPステータス404エラーを取得しますか?事前に感謝します

+0

' /' /* '、参照してくださいhttp://stackoverflow.com/questions/29792677 –

+0

' @RequestMapping( "/歓迎")へ 'を変更してみてください'エラーの理由は'/'に変わって作業をする、またはそれに従うコメント – emotionlessbananas

+0

あなたのプロジェクトのファイル構造は何ですか?あなたのjspビューのフォルダパスはどこにありますか? –

答えて

0

あなたのディスパッチャコントローラがあなたを見つけることができないため、HTTP 404を受信しました。

あなたのビューは、WEB-INFフォルダの下にあるlibフォルダの下にあります。

あなたがする必要があるのではなく、あなたが書いたもので、あなたがリゾルバを表示再構成である。

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
    <property name="prefix"> 
     <value>/WEB-INF/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

はこのようにそれを書く:はlibフォルダから

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">   
    <property name="prefix" value="/WEB-INF/lib/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 
をビューを抽出するために、あなたのビューリゾルバを設定

もう1つは、jspファイルをWEB-INFの下のビューフォルダの下に置き、libフォルダの下に置かないことです。

WEB-INF下のlibは、サードパーティのライブラリとjar専用フォルダです。

+0

実際にhello.jspはWEB-INFフォルダの下にあります...今、私はケプラーにeclipseネオンを変更し、新しいプロジェクトを開始して、良いコードを実行しています。 ..私は1つのものをそこに見つけました...スクリーンショットでここに私はspring-dispatcher-servlet.xmlファイルに小さな点を見ましたが、このドット記号はケプラープロジェクトにはありません。それはEclipseのネオンのバージョンやその他の問題ですか? –

関連する問題