使用注解实现 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 从未加载。 这是正确的方法吗?
谢谢 达努什
This may be trivial. But I didn't get any info on this.
Can we have an annotated Controller implementation class and a Controller implementation class that implements Controller interface(or extends AbstractController) in the same web application?
If so what is the way to do it.
I tried writing the following spring context, but the class that implements Controller was never loaded
<?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>
In my case the HomePageController is never loaded.
Is this the right way?
Thanks
Dhanush
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有效地禁用旧控制器。您需要通过声明UPDATE: 来启用它们。
DispatcherServlet
的功能由多种策略类控制。特别是,HandlerMapping 定义了将 URL 映射到控制器的方法,HandlerAdapter 定义了执行特定控制器调用的方法。因此,上面的行声明了能够将 URL 映射到 bean 名称并调用
Controller
类的策略。实际上,默认情况下会启用这些策略,但前提是没有显式声明其他策略。由于
在自己的策略中显式声明,因此您也需要显式声明这些 bean。另请参阅
DispatcherServlet
javadoc。<mvc:annotation-driven>
effectively disables old controllers. You need to enable them by declaringUPDATE: Functionality of
DispatcherServlet
is controlled by sevaral kinds of strategy classes. In particular,HandlerMapping
defines a way to map URLs onto controllers, andHandlerAdapter
defines a way to perform a call of particular controller.So, lines above declare strategies that enable mapping URLs onto bean names and calling
Controller
classes. Actually, there strategies are enabled by default, but only if no other strategies are declared explicitly. Since<mvc:annotation-driven>
declares in own strategies explicitly, you need to declare these beans explicitly as well.Also see
DispatcherServlet
javadoc.