Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

Implementing Spring MVC Controller using annotation as well as implementing Controller

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

Upvotes: 1

Views: 1101

Answers (1)

axtavt
axtavt

Reputation: 242686

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

Upvotes: 3

Related Questions