Struts LookupDispatchAction Example Application
LookupDispatchAction class can group the similar functions in to a single action class by using propeties file and overriding getKeyMethodMap() of HashMap.
In this example we can learn how to group set of userdefined methods in to a single action class.
LookupDispatchAction class extends org.apache.struts.actions.DispatchAction.Our class CalculateAction should extens to LookupDispatchAction.This class does not provide any predefined action methods.The user defined methods will be defined here.
In previous example of DispatchAction class the reqest parameter carry the value of the method directly.But in LookupDispatchAction class the value will be passed to method name using keys and values defining in getKeyMethodMap method.We should override this method in LookupDipatchAction class.This method returns map object with keys and values.
The additional elements required in LookupDispatchAction class:
Write the jsp page which is having four methods in a sinle action class as Addition(add),Substaction(sub),Multiplication(mul),Division(div).By entering two values in text boxes we have to calculate the result based on the operation.
In this page we have to create four buttons and their button names are taking by writing bean:message bean taglibrary tags.The properties file having this keys as the messages when you run the application it retreives the correspondent values from the ApplicationResources values on the buttons to display.
In ApplicationResource.properties file each key is mapped to a value, that value represents the button name in the jsp page. In the getKeyMethodMap( ) method the same key is mapped to a different value, this value corresponds to the method name to be invoked in the action class.
By executing the perticular method in Aciton class it returns a string of ActionForwards.Based on that string ActionController forwards the jsp page.
The performed action on button in jsp it will gather the correspondent name of the method and it will execute.In future we can change the names of the buttons with our touching the jsp page using ApplicationResources.properties file.This is the main advantage.
When the "add" button is clicked the request parameter value will be "method=btn.add" and it will invoke the correspondent "add" method in the CalculateAction.
In this example we can learn how to group set of userdefined methods in to a single action class.
LookupDispatchAction class extends org.apache.struts.actions.DispatchAction.Our class CalculateAction should extens to LookupDispatchAction.This class does not provide any predefined action methods.The user defined methods will be defined here.
In previous example of DispatchAction class the reqest parameter carry the value of the method directly.But in LookupDispatchAction class the value will be passed to method name using keys and values defining in getKeyMethodMap method.We should override this method in LookupDipatchAction class.This method returns map object with keys and values.
The additional elements required in LookupDispatchAction class:
- ApplicationResoureces.properties
- overriding getKeyMethodMap( ) in Action class.
Write the jsp page which is having four methods in a sinle action class as Addition(add),Substaction(sub),Multiplication(mul),Division(div).By entering two values in text boxes we have to calculate the result based on the operation.
<html:form action="/cal"> <table> <tr> <td width="70%">Enter First Number:<html:text property="fnos"></html:text><br> Enter Second Number:<html:text property="snos"></html:text><br> <html:submit property="method"> <bean:message key ="btn.add"/> </html:submit> <html:submit property="method"> <bean:message key = "btn.sub"/> </html:submit> <html:submit property="method"> <bean:message key = "btn.mul"/> </html:submit> <html:submit property="method"> <bean:message key = "btn.div"/> </html:submit> </td> <td> <img src="images/calculator.jpg" height="100" width="150"></img> </td> </tr> </table> </html:form>
In this page we have to create four buttons and their button names are taking by writing bean:message bean taglibrary tags.The properties file having this keys as the messages when you run the application it retreives the correspondent values from the ApplicationResources values on the buttons to display.
# -- myfile.properties -- btn.add=Add btn.sub=Sub btn.mul=Mul btn.div=DivThe ActionServlet invoks the struts-config.xml based on action mappings CalculateActionn will invoke to process and directly it checks in getKeyMethodMap() having the HashMap with keys and values.The names in the resource bundle file are the keys for this map and the corresponding values are the method names in the action class. For example the key value in the ApplicationResource.properties file is "btn.add" and the corresponding method name is "add" in the CalculateAction class. The main constraint in the DispatchAction is the method name in the action class and the button name in the jsp page should be the same. But here in LookupDispatchAction, we can have different names for the buttons and the methods.
package com.javabynataraj.lookupdispatch; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.LookupDispatchAction; /**author: muralidhar nayani * http://javabynataraj.blogspot.com */ public class CalculateAction extends LookupDispatchAction{ private int a = 0; private int b = 0; private int c1 = 0; Calculatedto c; ArrayList<Calculatedto> al=null; protected Map getKeyMethodMap(){ Map map = new HashMap(); map.put("btn.add", "add"); map.put("btn.sub", "sub"); map.put("btn.mul", "mul"); map.put("btn.div", "div"); return map; } public ActionForward add(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ CalculateForm cf = (CalculateForm)form; a = cf.getFnos(); b = cf.getSnos(); c1 = (a+b); Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("add", al); return maping.findForward("add1"); } public ActionForward sub(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ CalculateForm cf = (CalculateForm)form; a = cf.getFnos(); b = cf.getSnos(); if(a>b){ c1 = (a-b); Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("sub", al); } else{ c1 = (b-a); Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("sub", al); } return maping.findForward("sub1"); } public ActionForward mul(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ CalculateForm cf = (CalculateForm)form; a = cf.getFnos(); b = cf.getSnos(); c1 = (a*b); Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("mul", al); return maping.findForward("mul1"); } public ActionForward div(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ CalculateForm cf = (CalculateForm)form; a = cf.getFnos(); b = cf.getSnos(); if(b==0){ c1 = 0; Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("div", al); } else{ c1 = (a/b); Calculatedto c=new Calculatedto(); c.setResult(c1); c.setFnos(a); c.setSnos(b); al=new ArrayList<Calculatedto>(); al.add(c); HttpSession session=request.getSession(); session.setAttribute("div", al); } return maping.findForward("div1"); } }
In ApplicationResource.properties file each key is mapped to a value, that value represents the button name in the jsp page. In the getKeyMethodMap( ) method the same key is mapped to a different value, this value corresponds to the method name to be invoked in the action class.
By executing the perticular method in Aciton class it returns a string of ActionForwards.Based on that string ActionController forwards the jsp page.
<action-mappings> <action name="Calculator" path="/cal" parameter="method" input="Calculate.jsp" scope="session" type="com.javabynataraj.lookupdispatch.CalculateAction"> <forward name="add1" path="/add.jsp" /> <forward name="sub1" path="/sub.jsp" /> <forward name="mul1" path="/mul.jsp" /> <forward name="div1" path="/div.jsp" /> </action> </action-mappings> <message-resources parameter="com.javabynataraj.lookupdispatch.ApplicationResource" />
The performed action on button in jsp it will gather the correspondent name of the method and it will execute.In future we can change the names of the buttons with our touching the jsp page using ApplicationResources.properties file.This is the main advantage.
When the "add" button is clicked the request parameter value will be "method=btn.add" and it will invoke the correspondent "add" method in the CalculateAction.
Note1:Remember to configure ApplicationResources.properties in struts-config.xml
<message-resources parameter="com.javabynataraj.lookupdispatch.ApplicationResource" />
Note2: Configure all taglibraries in web.xml in jspconfig tag.Configure what ever we used like html,bean tld files.
The Application looks like:/WEB-INF/struts-html.tld /WEB-INF/struts-html.tld /WEB-INF/struts-bean.tld /WEB-INF/struts-bean.tld
After entering the values and perform action it will do the all internal LookupDispatchAction mechanism and displays as:
For Addition:
For Substraction:
For Division:
Download SOURCE
+