0

私はJavaとSpring Frameworkが初めてです。Tomcat 7 で Spring STS 3.1.0.RELEASE を使用しています。次の指示に従って Spring MVC プロジェクトを作成しています。ファイル > Spring テンプレート プロジェクト > Spring MVC プロジェクト。次は web.xml ファイル コードです。

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

以下は servlet-contxt.xml ファイルです。

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="org.dave.foo" />



</beans:beans>

コントローラーのデフォルトの名前と場所を変更したくありません。この目的のために、新しいパッケージ org.dave.bar を作成し、IndexController をこのパッケージに移動します。また、servlet-context.xml ファイルで以下のように変更します。

<context:component-scan base-package="org.dave.bar" />

しかし、Web サーバーを再起動し、この URL (http://localhost:8080/bar/) でコードを実行すると、404 エラーが発生します。誰かが私が間違っていることと、それを修正する方法を教えてくれますか?

前もって感謝します

4

1 に答える 1

0

最初に web.xml で、Spring MVC ディスパッチャー サーブレットで処理する URL マッピングを定義する必要があります (コントローラーのリクエスト マップ メソッドを介して)。あなたの質問で私が理解していることから、URLマッピングを次のように設定する必要があります(web.xmlで)

   <servlet>  
     <servlet-name>spring</servlet-name>  
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/*</url-pattern>  
   </servlet-mapping>  

次に、org.dave.foo の下に、Spring MVC コントローラー (@Controller でアノテーションが付けられたクラスと URL マップされたメソッド) を配置する必要があります。コントローラーでは、Spring ディスパッチャー サーブレット URL マッピング (web.xml DispatcherServlet で定義) のルートにマップされたメソッドが必要になります。したがって、上記の定義では、次のようなメソッドが必要になります。

@RequestMapping(value = "/")  
 public ModelAndView index(HttpServletRequest request,  
         HttpServletResponse response) {  
        ...
  }

一般に、最も具体的なリクエスト マッピングが「優先」されることに注意してください。これは、「/」リクエスト マッピングに加えて、コントローラに「/abc」のリクエスト マッピングがある場合、リクエスト localhost/myapp/abc は localhost/myapp/ を「獲得」することを意味します。

ここでより詳細な例を見ることができます: http://www.commonj.com/blogs/2012/11/30/spring-mvc-default-controller-name/

それが役に立ったことを願っています。

于 2012-11-30T13:07:55.633 に答える