2017-06-06 12 views
0

私はthis lessonですが、正常に動作する前にコントローラがerror 404で動作しません。 jspファイルはうまくいきます。コントローラーだけが作動しません。コンソールにエラーはありません。 何が起こったのですか?コントローラがSpring Webエラーで動作しない404

コントローラー:

@Controller 
public class HelloController { 
    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public String printHello(ModelMap model) { 
     model.addAttribute("message", "Hello Spring MVC Framework!"); 
     return "hello"; 
    } 
} 

の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>Servlets</display-name> 
    <servlet> 
     <servlet-name>HelloWeb</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HelloWeb</servlet-name> 
     <url-pattern>*.jsp</url-pattern> 
    </servlet-mapping> 
</web-app> 

HelloWeb-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" 
    xsi:schemaLocation = "http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

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

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

</beans> 

Hello.jspの

<html> 
    <head> 
     <title>Hello Spring MVC</title> 
    </head> 

    <body> 
     <h2>${message}</h2> 
    </body> 
</html> 

Folder structure

+0

オーケーを打ってみてくださいを実行し、それらの変更を行います。 tomcat/jettyの作業ディレクトリを確認してください。クラスファイルが存在しない可能性があります。作業スペースを更新し、tomcat/jettyの作業ディレクトリをクリアする/ 'mvn clean install'を実行する – harshavmb

+0

@harshavmb tnx、それはやったがまだ動作しない –

+0

tomcat/jetty作業ディレクトリをチェックしましたか?プロジェクトディレクトリ内にクラスディレクトリがあります。あなたはコンパイルされたHelloControllerクラスを見ていますか? – harshavmb

答えて

1

私が気付いていることはいくつかあります。
1)web.xmlファイル内の<?xmlタグ宣言は実際には許可されていません。 Eclipse内のJSPエディタでエラーが示されているはずです。削除してください<?xml version="1.0" encoding="UTF-8"?>

2)Dispatcherサーブレットのデフォルトサーブレットコンテナハンドラとマッピングの問題が発生していると思います。あなたは春のディスパッチャのための取り扱いデフォルトで

<servlet-mapping> 
    <servlet-name>HelloWeb</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>HelloWeb</servlet-name> 
    <url-pattern>*.jsp</url-pattern> 
</servlet-mapping> 

からweb.xmlファイル内のサーブレットマッピングを変更することができます。

私は、あなたのプロジェクトの再構築をあなたのコンテナに再デプロイして、コントローラのマッピングhttp://<yourhost_root>/HelloWeb/hello/

関連する問題