MultiActionController with Command Class
How one
can use command class Object while working with MultiActionController?
MultiActionController
is used to group logically related behaviours (operations) in single
Controller. It is something like struts DispatchAction but provides more
flexibility.
In below
example take two numbers from user and display result as addition or subtraction
or multiplication based on which button is clicked. All of three operations are
handled by a single Controller class.
Steps
1.
Create a new project and add Spring Web MVC
library
2.
Create input page like below
Index.jsp
<body>
<h3> Enter Numbers:</h3>
<form action="send.htm"
method="post">
Num1<input type="text" name="num1"/><br>
Num2<input type="text" name="num2"/><br>
<p>
<input type="submit" value="add"
name="method">
<input type="submit" value="sub"
name="method">
<input type="submit" value="mult" name="method"></p>
</form>
</body>
3.
Create a Controller class by sub classing
MultiActionController class and provides method for all above three operations which
are defined in index.jsp page. Method name must be the same as value of submit
button with two essential parameters request, response and third optional
parameter is name of command class.
public class MyController extends
MultiActionController {
public ModelAndView add(HttpServletRequest
request, HttpServletResponse respone, Num num) {
int n1= num.getNum1();
int n2= num.getNum2();
int n3=n1+n2;
return new ModelAndView("home", "msg",
"Sum=" + n3);
}
public
ModelAndView sub(HttpServletRequest request, HttpServletResponse respone, Num num) {
int n1= num.getNum1();
int n2= num.getNum2();
int n3=n1-n2;
return new ModelAndView("home", "msg",
"Sub=" + n3);
}
public ModelAndView mult(HttpServletRequest
request, HttpServletResponse respone, Num num) {
int n1= num.getNum1();
int n2= num.getNum2();
int n3=n1*n2;
return new ModelAndView("home", "msg",
"Mult=" + n3);
}
}
4.
Create command class by sub classing
MultiActionController class and provides getter and setter methods.
public class Num extends
MultiActionController
{
int num1;
int num2;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
}
5.
Configure ParameterMethodNameResolver class to
resolve method name in Controller
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop
key="index.htm">indexController</prop>
<prop
key="send.htm">multiController</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" />
<!--
The multi action controller.
-->
<bean id="multiController" class="pack.MyController">
<property name="methodNameResolver">
<ref bean="mresolver"/>
</property>
</bean>
<!--
Method resolver
-->
<bean id="mresolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>method</value>
</property>
</bean>
</beans>
6.
Create output page home.jsp to view result
<body>
<h1>${msg}</h1>
</body>
You might notice the difference between usage of command
class in SimpleFormController and MultiActionController.
Thank bro. It helps me too much...
ReplyDelete