Here I am explaining how to do switch action and how to use more than one config files in struts.
Switchform
package com.switchdispatch;
import org.apache.struts.action.ActionForm;
/**
*
* @author Prasanthi
*
*/
public class Switchform extends ActionForm{
int n1;
int n2;
String method;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
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;
}
}
Switchaction
package com.switchdispatch;
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.DispatchAction;
/**
*
* @author Prasanthi
*
*/
public class Switchaction extends DispatchAction{
public ActionForward add(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
Switchform l1=(Switchform)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)
{
Switchform l1=(Switchform)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");
}
}
Testform
package com.switchdispatch;
import org.apache.struts.action.ActionForm;
/**
*
* @author Prasanthi
*
*/
public class TestForm extends ActionForm{
String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Testaction
package com.switchdispatch;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author Prasanthi
*
*/
public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
TestForm form1=(TestForm)form;
String msg=form1.getMsg();
return(mapping.findForward("success"));
}
}
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="switchform" type="com.switchdispatch.Switchform" />
</form-beans>
<action-mappings>
<action
path="/switch"
type="org.apache.struts.actions.SwitchAction"/>
<action path="/switchtest"
type="com.switchdispatch.Switchaction"
name="switchform"
input="/SwitchTest.jsp"
parameter="method">
<forward name="add"
path="/switch.do?page=/test1.do&prefix=/test"
contextRelative="true" redirect="true"/>
<forward name="delete" path="/delete.jsp" contextRelative="true" redirect="true"/>
</action>
</action-mappings>
</struts-config>
test-config.xml
<struts-config>
<form-beans>
<form-bean name="testform1" type="com.switchdispatch.TestForm"/>
</form-beans>
<action-mappings>
<action path="/test1" name="testform1" input="/test1.jsp" type="com.switchdispatch.TestAction" scope="request">
<forward name="success" path="/hello.jsp" contextRelative="true" redirect="true" />
</action>
</action-mappings>
</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>
<init-param>
<param-name>config/test</param-name>
<param-value>/WEB-INF/test-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>SwitchTest.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>
Switchtest.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<html:html>
<body>
<html:form action="/switchtest">
<center>Add will switch to test-config.xml.<br>
<h2><u>Switch Form</u></h2><br>
<br>
Number1:<html:text property="n1" value=""/><br><br>
Number2:<html:text property="n2" value=""/><br><br>
Select<select name="method" >
<option value="add">Add</option>
<option value="delete">Delete</option>
<br/>
</select>
<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>
hello.jsp
<br><br>
<center>
<h2>Welcome To Switch Action</h2>
1 comment:
thanks good work
Post a Comment