i am trying multi controller in Springs 3.1.1 framework using annotation. but its not working i tried enabling support for annotation by configuring xml file in different ways. here is my code
dispatcher-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.web" />
<bean id="userKey" class="controllers.UserController" />
<bean id="newUserKey" class="controllers.UserFormController" />
<bean id="demoKey" class="controllers.demoController" />
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="user.htm">userKey</prop>
<prop key="add_user.htm">newUserKey</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Controller:
@Controller
public class multi extends MultiActionController {
@RequestMapping("/multi/add")
public ModelAndView add(){
return new ModelAndView("demo", "message", "Add method called");
}
@RequestMapping("/user/divide.htm")
public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("demo", "message", "divide method called");
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
I tried this:
Solutions 1:
<mvc:annotation-driven />
Solution 2:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
For starters you are mixing 2 strategies you have a Controller
which is annotated with @Controller
now which one should take precedence? Fix your controller by not extending MultiActionController
as this is intended to not work with annotations.
@Controller
public class multi {
@RequestMapping("/multi/add")
public ModelAndView add(){
return new ModelAndView("demo", "message", "Add method called");
}
@RequestMapping("/user/divide.htm")
public ModelAndView divide(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("demo", "message", "divide method called");
}
}
Your solution 2 is using the older Spring 2.5 based @RequestMapping
way of things, you should use the RequestMappingHandlerMapping
and RequestMappingHandlerAdapter
which both are registered when you use <mvc:annotation-driven />
.
Basically what you need in your dispatcher-servlet.xml
is the following
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Scan only for @Controllers -->
<context:component-scan base-package="com.web" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller />
</context:component-scan>
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!-- The index controller. -->
<mvc:view-controller view-name="index" />
</beans>
This will register everything that is need to process the @Controller
annotation (the <context:component-scan ... />
and the @RequestMapping
annotations (the <mvc:annotation-driven />
. You don't need the SimpleUrlHandlerMapping
as that is to be used with Controller
and not with @Controller
at least as of Spring 3.x.