开发者

Implementing Spring MVC Controller using annotation as well as implementing Controller

开发者 https://www.devze.com 2023-02-24 08:08 出处:网络
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

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.spri开发者_如何转开发ngframework.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


<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消