Spring MVC注释配置问题

时间:2010-05-11 21:44:04

标签: java spring tomcat spring-mvc spring-annotations

我正在尝试改进我的spring mvc配置,以便不需要为我添加的每个servlet添加新的配置文件,但是我遇到了问题。我尝试使用this tutorial作为起点,但我遇到了一个我无法弄清楚的问题。

问题是,当我对我的servlet执行GET操作时,我收到了404错误。这是我的配置和来自Controller的代表性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>

应用context.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>

应用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类的片段(很多,但它们看起来都像这样):

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

谁能告诉我我做错了什么? 谢谢!

3 个答案:

答案 0 :(得分:3)

这里唯一不合适的是你已经为root webapp context 使用了相同的上下文配置文件。这几乎可以保证是一个坏主意,并会导致很多奇怪的行为。这可能是导致问题的原因。

ContextLoaderListener配置了contextConfigLocation <context-param>,并创建和管理根WebApplicationContext

ServletDispatcherServlet配置了contextConfigLocation <init-param>,并创建和管理servlet WebApplicationContext

WebApplicationContext是servlet appcontext的父级,即根WebApplicationContext中的任何bean对servlet WebApplicationContext中的那些bean都是可见的。

您的第一步应该是分离这些配置。在正确的位置使用正确的bean(例如,所有MVC内容都必须在servlet上下文中)。 在两者之间共享bean定义,它只会让人感到困惑和/或破坏。

答案 1 :(得分:0)

这不能直接回答你的问题,但我总是发现在Spring无法找出问题时启用调试日志记录会很有帮助。

以下是我在logging.properties文件中常用的两个:

org.springframework.beans.factory.support.level=FINEST
org.springframework.security.level=FINEST

答案 2 :(得分:0)

你的控制器中有多个GET RequestMapping吗?如果存在多个并且将它们解析为特定请求时存在歧义,则Spring不会映射到任何模糊的GET映射。