我想提高我的Spring MVC的配置,以便不要求每一个的servlet我添加一个新的配置文件,但我遇到了问题。我已经尝试使用本教程作为一个出发点,但我运行到一个问题,我想不通。

问题是,当我做了让我的servlet中,我得到一个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>

应用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
@RequestMapping("/foo.xml")
public class FooController
{       
    @RequestMapping(method=RequestMethod.GET)
    public void handleGET(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        ...

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

有帮助吗?

解决方案

出来的地方这里唯一的事情是,你已经使用了相同的情况下配置文件,使它们的根web应用上下文的的你的servlet上下文。这几乎肯定会是一个坏主意,并会导致很多怪异的行为。这很可能是您的问题的原因。

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

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

WebApplicationContext是servlet appcontext的父,即在根WebApplicationContext任何豆是在servlet WebApplicationContext那些豆可见。

您第一步应该是分开的那些配置。在正确的地方正确的豆类(如所有MVC的东西在servlet上下文去)。做的的两者之间共享的bean定义,它会一下就混乱和/或破碎。

其他提示

这并不直接回答你的问题,但我总是发现它有助于使春季调试日志记录时,我想不出什么错误。

下面是两个,我在我的logging.properties文件通常使用:

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

你任何机会在你的控制器不止一个GET RequestMapping?如果有一个以上的和有歧义在他们解析为特定请求Spring不映射到任何不明确的GET映射。

scroll top