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.