2

追加するすべてのサーブレットに新しい構成ファイルを必要としないように、Spring MVC 構成を改善しようとしていますが、問題が発生しています。このチュートリアルを出発点として使用してみましたが、理解できない問題に直面しています。

問題は、サーブレットに対して GET を実行すると、404 エラーが返されることです。これが私の構成と、コントローラーからの代表的な Java スニペットです。

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">
    <display-name>SightLogix Coordination System</display-name>

    <description>SightLogix Coordination System</description>

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/application-context.xml
                    /WEB-INF/application-security.xml
                </param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/slcs/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/application-context.xml
            /WEB-INF/application-security.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>
                org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

アプリケーションコンテキスト.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"

    default-init-method="init" default-destroy-method="destroy">

    <mvc:annotation-driven />

    <context:component-scan base-package="top.level" />
</beans>

application-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <http>
        <intercept-url pattern="/**" access="ROLE_MANAGER" requires-channel="https" />
        <http-basic />
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="myUserDetailsService">
            <password-encoder hash="sha"/>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="myUserDetailsService"
        class="path.to.my.UserDetailsServiceImpl">
    </beans:bean>

</beans:beans>

Controller クラスのスニペット (多数ある中の 1 つですが、基本的にはすべて次のようになります):

@Controller
@RequestMapping("/foo.xml")
public class FooController
{       
    @RequestMapping(method=RequestMethod.GET)
    public void handleGET(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        ...

誰かが私が間違っていることを教えてもらえますか? ありがとう!

4

3 に答える 3

3

ここで唯一不適切なのは、ルート webapp コンテキストサーブレット コンテキストの両方に同じコンテキスト構成ファイルを使用したことです。これは悪い考えであることがほぼ保証されており、多くの奇妙な動作が発生します。これが問題の原因である可能性があります。

ContextLoaderListenerで設定されcontextConfigLocation <context-param>、ルート を作成および管理しWebApplicationContextます。

ServletDispatcherServletで構成されcontextConfigLocation <init-param>、サーブレットを作成および管理しますWebApplicationContext

ルートWebApplicationContextは、サーブレット appcontext の親です。つまり、ルート内のすべての Bean はWebApplicationContext、サーブレット内のそれらの Bean から見えますWebApplicationContext

最初のステップは、これらの構成を分離することです。正しい Bean を正しい場所に配置します (たとえば、すべての MVC はサーブレット コンテキストに配置する必要があります)。2 つの間で Bean 定義を共有しないでください。混乱したり壊れたりするだけです。

于 2010-05-11T22:19:22.210 に答える
0

これはあなたの質問に直接答えるものではありませんが、何が問題なのか分からない場合、Spring でデバッグ ログを有効にすると役立つことが常にわかっています。

以下は、logging.properties ファイルでよく使用する 2 つです。

org.springframework.beans.factory.support.level=FINEST
org.springframework.security.level=FINEST
于 2010-05-11T21:52:08.507 に答える
0

ひょっとして、コントローラーに複数あることはありませんGET RequestMappingか? 複数あり、それらを特定のリクエストに解決する際にあいまいさがある場合、Spring はあいまいな GET マッピングのいずれにもマップしません。

于 2010-05-27T21:07:08.140 に答える