Chaining actions can be done by simply using the proper mapping in your forward entries in the struts-config.xml file. Assume you had the following two classes:
/* com/AAction.java */ ...
public class AAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Do something
return mapping.findForward("success"); } }
/* com/BAction.java */ ...
public class BAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Do something else
Then you can chain together these two actions with the Struts configuration as shown in the following excerpt:
... <action-mappings type="org.apache.struts.action.ActionMapping"> <action path="/A" type="com.AAction" validate="false"> <forward name="success" path="/B.do" /> </action> <action path="/B" type="com.BAction" scope="session" validate="false"> <forward name="success" path="/result.jsp" /> </action> </action-mappings> ...
Here we are assuming you are using a suffix-based (.do) servlet mapping, which is recommended since module support requires it. When you send your browser to the web application and name the action x.do (i.e. http://localhost:8080/app/x.do) it will execute AAction.execute(), which will then forward to the "success" mapping. This causes the execution of BAction.execute() since the entry for "success" in the configuration file uses the .do suffix. Of course it is also possible to chain actions programmatically, but the power and ease of being able to "reroute" your web application's structure using the XML configuration file is much easier to maintain. A
ActionForms are added to a servlet scope (session or request) as beans. What this means is that, for certain functionality to be available, your ActionForms will have to follow a few simple rules. First, your ActionForm bean must have a zero-arguments constructor. This is required because Struts must be able to dynamically create new instances of your form bean class, while knowing only the class name. This is not an onerous restriction, however, because Struts will also populate your form bean's properties (from the request parameters) for you. Second, the fields of your form bean are made available to the framework by supplying public getter and setter methods that follow the naming design patterns described in the JavaBeans Specification. For most users, that means using the following idiom for each of your form bean's properties: private {type} fieldName; public {type} getFieldName() { return (this.fieldName); }
public void setFieldName({type} fieldName) { this.fieldName = fieldName; } NOTE - you MUST obey the capitalization conventions shown above for your ActionForm properties to be recognized. The property name in this example is "fieldName", and that must also be the name of the input field that corresponds to this property. A bean property may have a "getter" method and a "setter" method (in a form bean, it is typical to have both) whose name starts with "get" or "set", followed by the property name with the first character capitalized. (For boolean properties, it is also legal to use "is" instead of "get" as the prefix for the getter method.) Advanced JavaBeans users will know that you can tell the system you want to use different names for the getter and setter methods, by using a java.beans.BeanInfo class associated with your form bean. Normally, however, it is much more convenient to follow the standard conventions. WARNING - developers might be tempted to use one of the following techniques, but any of them will cause your property not to be recognized by the JavaBeans introspection facilities, and therefore cause your applications to misbehave: * Using getter and setter method names that do not match - if you have a getFoo() method for your getter, but a setBar() method for your setter, Java will not recognize these methods as r
You can submit a form with a link as below. BTW, the examples below assume you are in an block and 'myForm' is picked up from the struts-config.xml name field of the action. <a href='javascript:void(document.forms["myForm"].submit()>My Link</a> Now the trick in the action is to decode what action you intend to perform. Since you are using JavaScript, you could set a field value and look for it in the request or in the form. ... html/javascript part ... <input type='hidden' value='myAction' /> <input type='button' value='Save Meeeee' onclick='document.forms["myForm"].myAction.value="save"; document.forms["myForm"].submit();' /> <input type='button' value='Delete Meeeee' onclick='document.forms["myForm"].myAction.value="delete"; document.forms["myForm"].submit();' /> ... the java part ... class MyAction extends ActionForm implements Serializable {
public ActionForward execute (ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse) {
String myAction = req.getParameter("myAction");
if (myAction.equals("save") { // ... save action ... } else if (myAction.equals("delete") { // ... delete action ... } } } } This is just one of many ways to achieve submitting a form and decoding the intended action. Once you get used to the framework you will find other ways that make more sense for your coding style and requirements. Just remember this example is completely non-functional without JavaScript.
The <html:javascript> tag allows front-end validation based on the xml in validation.xml.
For example the code:
generates the client side JavaScript for the form "logonForm" as defined in the validation.xml file. The <html:javascript> when added in the JSP file generates the client side validation script.
The basic idea is a series of actions with next, back, cancel and finish actions with a common bean. Using a LookupDispatchAction is reccomended as it fits the design pattern well and can be internationalized easily. Since the bean is shared, each choice made will add data to the wizards base of information. A sample of struts-config.xml follows:
< form-beans> <form-bean name="MyWizard" type="forms.MyWizard" /> </form-beans>
<!-- the first screen of the wizard (next action only available) --> <!-- no validation, since the finish action is not available --> <actions> <action path="/mywizard1" type="actions.MyWizard" name="MyWizard" validate="false" input="/WEB-INF/jsp/mywizard1.jsp"> <forward name="next" path="/WEB-INF/jsp/mywizard2.jsp" /> <forward name="cancel" path="/WEB-INF/jsp/mywizardcancel.jsp" /> </action>
<!-- the second screen of the wizard (back, next and finish) --> <!-- since finish action is available, bean should validated, note validation should not necessarily validate if back action requested, you might delay validation or do conditional validation --> <action path="/mywizard2" type="actions.MyWizard" name="MyWizard" validate="true" input="/WEB-INF/jsp/mywizard2.jsp"> <forward name="back" path="/WEB-INF/jsp/mywizard1.jsp" /> <forward name="next" path="/WEB-INF/jsp/mywizard3.jsp" /> <forward name="finish" path="/WEB-INF/jsp/mywizarddone.jsp" /> <forward name="cancel" path="/WEB-INF/jsp/mywizardcancel.jsp" /> </action>
<!-- the last screen of the wizard (back, finish and cancel only) --> <action path="/mywizard3" type="actions.MyWizard" name="MyWizard" validate="true" input="/WEB-INF/jsp/mywizard3.jsp"> <forward name=&q
A Great Tutorials Portal
Aquarian Infotech System
Copyright © 2008 interviewmaterial.com. All rights reserved.