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

发布于 2024-11-01 05:16:57 字数 1952 浏览 0 评论 0原文

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

我们可以在同一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-11-08 05:16:57

有效地禁用旧控制器。您需要通过声明

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

UPDATE: 来启用它们。 DispatcherServlet 的功能由多种策略类控制。特别是,HandlerMapping 定义了将 URL 映射到控制器的方法,HandlerAdapter 定义了执行特定控制器调用的方法。

因此,上面的行声明了能够将 URL 映射到 bean 名称并调用 Controller 类的策略。实际上,默认情况下会启用这些策略,但前提是没有显式声明其他策略。由于 在自己的策略中显式声明,因此您也需要显式声明这些 bean。

另请参阅 DispatcherServletjavadoc

<mvc:annotation-driven> effectively disables old controllers. You need to enable them by declaring

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

UPDATE: Functionality of DispatcherServlet is controlled by sevaral kinds of strategy classes. In particular, HandlerMapping defines a way to map URLs onto controllers, and HandlerAdapter 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文