ForwardAction
Previous | Home | Next |
ForwardAction acts as bridge from the current view and pages it link to.ForwardAction uses the RequestDispatcher to forward to a specified web resource.This allow you to link to an action instead of directly to a jsp.
The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions. ForwardAction used to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page.Following step are required in ForwardAction :-
Create the html:link tag with the action attrributes ,add a link to the jsp pages that points to the action.<html:link action="home">Login </html:link>Create an action mapping that uses the ForwardAction with teh parameter attributes to specify the jsp pages.Directory Structure of ForwardActionExample in Struts 1.3 Using MyEclipse IDE
index.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title><bean:message key="label.title"/></title> </head> <body> <h3><bean:message key="label.header"/></h3> <html:form action="/forwardAction" > <table cellpadding="3" cellspacing="3"> <tr><td><bean:message key="label.name"/></td><td><html:text name="forwardForm" property="name" /></td></tr> <tr><td><bean:message key="label.address"/></td><td><html:text name="forwardForm" property="address" /></td></tr> <tr><td><bean:message key="label.city"/></td><td><html:text name="forwardForm" property="city" /></td></tr> <tr><td><bean:message key="label.state"/></td><td><html:text name="forwardForm" property="state" /></td></tr> <tr><td><bean:message key="label.country"/></td><td><html:text name="forwardForm" property="country" /></td></tr> <tr><td><html:submit><bean:message key="label.submit"/></html:submit></td></tr> </table> </html:form> </body> </html>web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <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>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>struts-config.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="forwardForm" type="org.r4r.struts.ForwardForm"/> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action path="/forward" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/> <action path="/forwardAction" name="forwardForm" type="org.r4r.struts.MyForwardAction" input="/index.jsp"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>ForwardForm.javapackage org.r4r.struts; import org.apache.struts.action.ActionForm; @SuppressWarnings("serial") public class ForwardForm extends ActionForm { private String name; private String address; private String city; private String state; private String country; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }MyForwardAction.javapackage org.r4r.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class MyForwardAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } }ApplicationResources.properties# Resources for parameter 'org.r4r.struts.ApplicationResources' # Project Struts1.3_ForwardActionExample label.title=Struts 1.3 ForwardAction Example label.header=Struts 1.3 ForwardAction Example label.name=Name label.address=Address label.city=City label.state=State label.country=Country label.submit=Submitsuccess.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title><bean:message key="label.title"/></title> </head> <body> <h3><bean:message key="label.header"/></h3> <table cellpadding="3" cellspacing="3" border="1"> <tr><td><bean:message key="label.name"/>:-</td><td><bean:write name="forwardForm" property="name"/></td></tr> <tr><td><bean:message key="label.address"/>:-</td><td><bean:write name="forwardForm" property="address"/></td></tr> <tr><td><bean:message key="label.city"/>:-</td><td><bean:write name="forwardForm" property="city"/></td></tr> <tr><td><bean:message key="label.state"/>:-</td><td><bean:write name="forwardForm" property="state"/></td></tr> <tr><td><bean:message key="label.country"/>:-</td><td><bean:write name="forwardForm" property="country"/></td></tr> </table> </body> </html>Output
Previous | Home | Next |