Here I am explaining how to do Lookup Dispatch Action in struts .
LookupForm
package com.lookupdispatch;
import org.apache.struts.action.ActionForm;
/**
*
* @author Prasanthi
*
*/
public class LookupForm extends ActionForm{
int n1;
int n2;
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
}
LookupAction
package com.lookupdispatch;
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 Prasanthi
*
*/
public class LookupAction extends LookupDispatchAction{
protected Map getKeyMethodMap() {
Map map=new HashMap();
map.put("submit.add","add");
map.put("submit.delete","delete");
return map;
}
public ActionForward add(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
LookupForm l1=(LookupForm)form;
int n1=l1.getN1();
int n2=l1.getN2();
HttpSession ses=req.getSession();
int tot=n1+n2;
ses.setAttribute("tot",Integer.toString(tot));
return map.findForward("add");
}
public ActionForward delete(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
LookupForm l1=(LookupForm)form;
int n1=l1.getN1();
int n2=l1.getN2();
HttpSession ses=req.getSession();
int tot=n1-n2;
ses.setAttribute("del",Integer.toString(tot));
return map.findForward("delete");
}
}
ApplicationResources.properties
submit.add=add
submit.delete=delete
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="lookupform" type="com.lookupdispatch.LookupForm" />
</form-beans>
<action-mappings>
<action path="/lookup"
type="com.lookupdispatch.LookupAction"
name="lookupform"
input="/LookupTest.jsp"
parameter="method">
<forward name="add" path="/add.jsp"/>
<forward name="delete" path="/delete.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
web.xml
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>LookupTest.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
</web-app>
LookupTest.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<html:html>
<body>
<html:form action="/lookup">
<center>
<h2><u>LookUpForm</u></h2>
Number1:<html:text property="n1" value=""/><br><br>
Number2:<html:text property="n2" value=""/><br><br>
<html:submit property="method" value="add"></html:submit>
<html:submit property="method">delete</html:submit>
</html:form>
</body>
</html:html>
add.jsp
<html>
<center>
In ADD Action<br>
<% String st=(String)session.getAttribute("tot");%>
Total is <%=st%>
</html>
delete.jsp
<html>
<center>In Delete Action<br>
<% String st=(String)session.getAttribute("del");%>
After Subtraction <%=st%>
</html>
No comments:
Post a Comment