java - Spring MVC注解配置问题

标签 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>

应用程序上下文.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>

应用程序安全.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
    {
        ...

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

最佳答案

这里唯一不合适的是,您为根 Web 应用程序上下文和 servlet 上下文使用了相同的上下文配置文件。这几乎肯定是一个坏主意,并且会导致很多奇怪的行为。这很可能是导致您出现问题的原因。

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

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

WebApplicationContext是 servlet appcontext 的父级,即根 WebApplicationContext 中的任何 beans对于 servlet 中的那些 beans 是可见的 WebApplicationContext .

您的第一步应该是分离这些配置。将正确的 bean 放在正确的位置(例如,所有 MVC 内容都必须放在 servlet 上下文中)。不要在两者之间共享 bean 定义,这只会造成困惑和/或损坏。

关于java - Spring MVC注解配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2814669/

相关文章:

java - PHP 相当于 JAVA HexUtils.bytesToHex()

用于从位于服务器中的 .ser 文件读取对象的 Java 程序

java - 使用 Spring 进行 hibernate 二级缓存

java - 如何使用WebClient执行同步请求?

java - SSO - 中央身份验证服务 (CAS) - 用于生产?

java - 什么时候写入访问日志?

javascript - 在 Karate DSL 中,在 Java 参数调用中传递变量时如何转义单引号

java - Eclipse - 生成 Getters/Setters

java - 是否可以在 ubuntu 上的 apache-tomcat-7.0.54 中配置代理设置?

Tomcat 无法启动