1

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That Worked. What does those two entries do in this case?
@Bozho - I could't upvote it since I didn't have enough reputation (15 is needed to upvote). I am a newbie in SO
@dhanush yes, I'm aware of that, don't worry. That's why I upvote it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.