Wednesday, 18 March 2009

Example : MultiActionController in Spring


1> Controller Contains
public class testController extends MultiActionController {
public ModelAndView action_name1(HttpServletRequest          httpServletRequest,HttpServletResponse httpServletResponse) {
System.out.println(“In Action1″);
return new ModelAndView(“view1″, “date”,”date”);
}
public ModelAndView action_name2(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
System.out.println(“In Action 2″);
return new ModelAndView(“view2″, “date”,”date”);
}
}
2> View contains
<a href=”test.htm?action=action_name1″ >Go to Action1 </a> |
<a href=”test.htm?action=action_name2″ >Go to Action2 </a>
3> Change in Application-servlet.xml
<bean name=”/test.htm” class=”testController”>
<property name=”methodNameResolver”>
<ref bean=”paramResolver”/>
</property>
</bean>
<bean id=”paramResolver” class=”org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver”>
<property name=”paramName”><value>action</value></property>
</bean>
Simple Way:
1> Controller Contains
same as above
2> View contains
<a href=”test.htm/action_name1″ >Go to Action1 </a> |
<a href=”test.htm/action_name2″ >Go to Action2 </a>
3> Change in Application-servlet.xml
No need to add anything