Dispatchaction is another Action class that Struts that can save a large amount of saving time. I want to use Dispatchction to make a function of adding, modifying, deleting records from forms.
ADD, Alert, delete, etc. in MyDispatchaction, how is the number of parameters when adding a form?
For example: Press the Add Button to implement myDispathaction? Method = add this to a submission?
1. Use dispatchaction
Dispatchaction is another Action class that Struts contains a large amount of saving time. Unlike other Action classes, only a single execute () method implements a single business, and DispatchAction allows you to write multiple methods related to your business in a single Action class. This reduces the number of Action classes and collects the relevant business methods to make maintenance easier.
To use Dispatchaction's features, you need to create a class yourself and get it by inheriting abstract dispatchaction. There must be a specific method Signature for each business method to provide. For example, we want to provide a method to implement a list of goods to the shopping cart, create a class ShoppingCartDispatchaction provides the following methods:
Then, this class is likely to need a deleteItem () method to remove a list of items from the customer's shopping cart, and the ClearCart () method clears the shopping cart and so on. At this time we can set these methods in a single Action class, without providing an Action class for each method.
When you call a method in the ShopPingCartDispatchaction, just provide the method name as the parameter value in the URL. That is to say, call addItem () Fang ǖ? URL looks like:
http: // myhost / storefront / action / cart? method = additem
The Method parameter specifies the method to be called in the ShoppingCartDispatchaction. The name of the parameter can be arbitrarily configured, "Method" used here is just an example. The name of the parameter can be set in the STRUTS configuration file.
2. Using LookUpdispatchaction
Org.apache.struts.Actions.lookupdispatchaction class:
Usually lookupdispatchaction is primarily applied to multiple submission buttons in a form, and these buttons have a common name, the names of these buttons, and the specific actionMApping parameter properties.
When configuring lookupdispatchaction, you should set the parameter property to "action" in the
Be a hidden variable.
Then use JS to judge
Function setAction (OPTYPE) {
Document.name1.action.value = OPTYPE
Document.name1.submit ();
}
// ->
Script>
hEAD>
οnclick = "setAction'updatedsp ');"> html: button> οnclick = "setAction'adddsp ';"> html: button> html: form> body> Html> Define an element of Hidden, the parameters submitted by JS control It is more chaotic with this class, and Form's form is annoyed. It can be used in the same way, this is used in the same page, which is used in multiple submission on the same page. If it is just a page submit, there is a simpler method. Plus a parameter is not the line ~ Verify this parameter in action, if you have form, add a getSet method, you can also verify that add a comment can also solve the problem. An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this Action in Your struts-config.xml file, create an entry like this: TYPE = "org.example.myaction" Name = "MyForm" Scope = "request" INPUT = "/ test.jsp" Parameter = "Method" /> Which Will Use The Value of The Request Parameter Named "Method" to Locate The Corresponding Key In ApplicationResources. for Example, You Might Have The Following ApplicationResources.properties: Button.add = add record Button.delete = delete record AND Your JSP Would Have The Following Format for Submit Buttons: html: Submit> html: Submit> html: form> Your Subclass Must Implement Both GetKeymethodMap and The Methods Defined in The Map. An Example of Such Implementations Are: protected map getKeyMethodMap () { Map map = new hashmap (); Map.Put ("Button.Add", "Add"); Map.Put ("Button.Delete", "delete"); Return Map; } Public ActionForward Add (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response THROWS IOException, servletexception { // Do Add Return mapping.findforward ("Success"); } Public ActionForward Delete (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response THROWS IOException, servletexception { // Do Delete Return mapping.findforward ("Success"); } Notes - if Duplicate Values EXIST for the Keys Returned By GetKeys, ONLY THE FIRST One Found Will Be Returned. If No Corresponding Key Is Found Ten An Exception Will Be Thrown. You Can Override THE Method unspecified to provide a custom handler. if the submit WAS Cancelle (a HTML: Cancel Button Was Pressed), The Custom Handler ca Zantelled will be used instead.