Dispatchaction, mappingdispatchaction, lookupdispatchaction

Source: Internet
Author: User

First, let's take a look at the relationship between the three of them.

Java. Lang. Object
|
+ -- Org. Apache. Struts. action. Action
|
+ -- Org. Apache. Struts. Actions. dispatchaction
|
+ -- Org. Apache. Struts. Actions. lookupdispatchaction
|
+ -- Org. Apache. Struts. Actions. mappingdispatchaction

Dispatchaction


Public abstract class dispatchaction extends action
This is an abstract action. It will execute corresponding methods according to the parameter in the request. Different actions can be grouped into an action file through this action class.

Struts-config.xml:
<Action Path = "/savesubject" type = "org. apache. struts. actions. dispatchaction "name =" subscriptionform "Scope =" request "input ="/subscribe. JSP "parameter ="Method"/>

There must be a corresponding method in action:
Public class demoaction extends dispatchaction {
Public actionforwardDelete(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardInsert(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardUpdate(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
}

You can access your program in this way:

Http: // localhost: 8080/MyApp/savesub.pdf. do?Method=Update

If the parameter in the parameter is null, the unspecified method in the action is executed.

Lookupdispatchaction


Public abstract class lookupdispatchaction extends dispatchaction
This action abstract class inherits dispatchaction, and the execution of its corresponding methods isThe parameter attribute is determined.. It is suitable for a form with many buttons, and different operations are performed by pressing different buttons.

Struts-config.xml:
<Action Path = "/test"
Type = "org. example. myaction"
Name = "myform"
Scope = "request"
Input = "/test. jsp"
Parameter ="Method"/>

Applicationresources. properties:

Button. Add = add record
Button. Delete = Delete record

In JSP:

<HTML: Form Action = "/test">
<HTML: Submit property ="Method">
<Bean: Message key = "button. Add"/>
</Html: Submit>
<HTML: Submit property ="Method">
<Bean: Message key = "button. Delete"/>
</Html: Submit>
</Html: Form>

In actionGetkeymethodmap must be implemented:

Protected map getkeymethodmap (){
Map map = new hashmap ();
Map. Put ("button. Add ","Add");
Map. Put ("button. Delete ","Delete");
Return map;
}

Public actionforwardAdd(Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws ioexception, servletexception {
// Do add
Return Mapping. findforward ("success ");
}

Public actionforwardDelete(Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws ioexception, servletexception {
// Do Delete
Return Mapping. findforward ("success ");
}

Mappingdispatchaction

Public class mappingdispatchaction extends dispatchaction
The corresponding method is executed by actionmappingParameter NameNote that, unlike lookupdispatchaction, the execution of corresponding methods of lookupdispatchaction is determined by the parameter attribute in actionmapping.

Action:

Public actionforwardCreate(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardEdit(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardSave(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardDelete(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforwardList(Actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

For which you wocould create corresponding <action> tolerations that reference this class:
 

Struts-config.xml:

<Action Path = "/createsub.pdf"
Type = "org. example. subscriptionaction"
Parameter ="Create">
<Forward name = "success" Path = "/editsubscription. jsp"/>
</Action>

<Action Path = "/editsubtasks"
Type = "org. example. subscriptionaction"
Parameter ="Edit">
<Forward name = "success" Path = "/editsubscription. jsp"/>
</Action>

<Action Path = "/savesub.pdf"
Type = "org. example. subscriptionaction"
Parameter ="Save"
Name = "subscriptionform"
Validate = "true"
Input = "/editsubpipeline. jsp"
Scope = "request">
<Forward name = "success" Path = "/savedsubscribe. jsp"/>
</Action>

<Action Path = "/deletesub.pdf"
Type = "org. example. subscriptionaction"
Name = "subscriptionform"
Scope = "request"
Input = "/subscribe. jsp"
Parameter ="Delete">
<Forward name = "success" Path = "/deletedsubscribe. jsp"/>
</Action>

<Action Path = "/listsubscriptions"
Type = "org. example. subscriptionaction"
Parameter ="List">
<Forward name = "success" Path = "/subscriptionlist. jsp"/>
</Action>

Dispatchaction, lookupdispatchaction, mappingdispatchaction
1) dispatchaction is to configure a form field name with the parameter in Struts-config. The value of this field is the final replacement of the method called by execute. for example, parameter = "method" and request. getparameter ("method") = "save", where "save" is methodname. Struts requests will be distributed to "save" or "edit" Or something based on parameter. However, the statement of the SAVE () or edit () method must be exactly the same as that of the execute method.
2) lookupdispatchaction inherits dispatchaction, which is used to send different responses to multiple submit buttons on the same page. First, use messageresource to associate the button text with the reskey, for example, button. save = save; then repeat getkeymethodmap () to match the reskey with the methodname, for example, map. put ("button. save "," save "); the configuration method is the same as that of dispatchaction, which must be written as follows:
3) mappingdispatchaction is newly added by 1.2 and also inherited from dispatchaction. its implementation of the function and the above two differences, is through the struts-config.xml to map multiple action-mapping to the same action class of different methods, the typical configuration is:

Then useraction inherits mappingdispatchaction, which includes:
Public actionforward save (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
Public actionforward edit (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception
And other methods
As you can see, no matter how the changes, these classes actually break down the Execute. Whether it is save, edit or other methods, they are actually equivalent to the original execute, there is no direct relationship between SAVE and edit. Actually, they are two different operations of the same business model. I think this is a problem. For the SAVE and edit requests, the background logic may be different from the method used to call the service, other codes are exactly the same (such as error processing and logging ). So I came up with this little thing and implemented local decomposition within the execute method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.