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

时间:2011-04-14 08:03:04

标签: spring-mvc

这可能是微不足道的。但我没有得到任何关于此的信息。

我们是否可以在同一个Web应用程序中使用带注释的Controller实现类和Controller实现类来实现Controller接口(或扩展AbstractController)?

如果是这样,那么它的方法是什么。

我尝试编写以下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。 这是正确的方法吗?

由于 丹努什

1 个答案:

答案 0 :(得分:3)

<mvc:annotation-driven>有效地禁用旧控制器。您需要通过声明

来启用它们
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

更新: DispatcherServlet的功能由各种策略类控制。特别是,HandlerMapping定义了一种将URL映射到控制器的方法,HandlerAdapter定义了一种执行特定控制器调用的方法。

因此,上面的行声明了将URL映射到bean名称并调用Controller类的策略。实际上,默认情况下会启用策略,但前提是没有明确声明其他策略。由于<mvc:annotation-driven>明确地在自己的策略中声明,因此您还需要明确声明这些bean。

另见DispatcherServlet javadoc

相关问题