使用注释实现Spring MVC Controller以及实现Controller

问题描述

| 这可能是微不足道的。但是我没有得到任何信息。 我们是否可以在同一Web应用程序中具有带注释的Controller实现类和一个实现Controller接口(或扩展AbstractController)的Controller实现类? 如果是这样,该怎么做。 我尝试编写以下spring上下文,但是从未加载实现Controller的类
    <?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:lang=\"http://www.springframework.org/schema/lang\"
    xmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:context=\"http://www.springframework.org/schema/context\"
    xmlns:mvc=\"http://www.springframework.org/schema/mvc\"
    xsi:schemaLocation=\"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd\">



    <bean name=\"/home.htm\" class=\"com.dell.library.web.HomePageController\">
        <property name=\"library\" ref=\"library\" />
    </bean>

    <context:component-scan base-package=\"com.dell.library.web.anot\" />
    <mvc:annotation-driven />

    <bean id=\"viewResolver\"
        class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">
        <property name=\"prefix\">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name=\"suffix\">
            <value>.jsp</value>
        </property>
    </bean>

</beans>
在我的情况下,HomePageController永远不会加载。 这是正确的方法吗? 谢谢 达努什     

解决方法

<mvc:annotation-driven>
有效禁用旧控制器。您需要通过声明启用它们
<bean class = \"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping\" />
<bean class = \"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter\" />
更新:
DispatcherServlet
的功能由几种策略类别控制。特别地,“ 4”定义了将URL映射到控制器上的方式,“ 5”定义了执行特定控制器的调用的方式。 因此,上面的几行声明了一些策略,这些策略使URL能够映射到bean名称上并调用“ 6”类。实际上,默认情况下会启用这些策略,但前提是没有明确声明没有其他策略。由于
<mvc:annotation-driven>
在自己的策略中显式声明,因此您也需要显式声明这些Bean。 另请参阅“ 3” javadoc。