Detailed description of dispatchaction

Source: Internet
Author: User

In-depth analysis of dispatchaction, lookupdispatchaction, and mappingdispatchaction

 

 

In-depth analysis of dispatchaction, lookupdispatchaction, and mappingdispatchaction

 

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

Definition

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 actionforward Delete (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward insert (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward Update (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.

 

Lookupdispatchactionpublic abstract class lookupdispatchaction extends dispatchaction

The action abstract class inherits the dispatchaction. The execution of the corresponding method is determined by the parameter attribute in actionmapping. 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

Button. Delete = Delete

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>

Getkeymethodmap must be implemented in action:

 

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 ");

}

Mappingdispatchactionpublic class mappingdispatchaction extends dispatchaction

The execution of corresponding methods is determined by the parameter name in actionmapping. Note that unlike lookupdispatchaction, the execution of corresponding methods of lookupdispatchaction is determined by the parameter attribute in actionmapping,

 

Struts-config.xml:

 

 

<Action Path = "/savesub.pdf"

Type = "org. example. subscriptionaction"

Name = "subscriptionform"

Scope = "request"

Input = "/subscribe. jsp"

Parameter = "method"/>

Action:

 

Public actionforward create (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward edit (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward save (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward Delete (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

Public actionforward list (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception

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

 

 

<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>

 
Tags: dispatchaction

 

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.

Some people mentioned above have seen similar ibatis code. Indeed, ibatis implements a beanaction, writes actionform and acion to a class, and passes the actionmapping, request, and response parameters between methods through a threadlocal local variable. The declaration of each method becomes very simple, and the return is also the string forward name.

I was inspired by Clinton begin to a large extent, but the beanaction implementation framework is slightly larger, rewriting the struts fundamental mode. The author also said that the framework has not been tested in practical use, so I don't want to use it.

The post on the roof is intended to be as comprehensive as possible, so I wrote all services and Dao into it. I don't quite understand why I don't need to use setdao. Is it possible to use spring for direct injection? In that case, the spring proxy must be used, and the configuration is not very stable and greatly increased.
Some people say that xxxdao is a local variable and linear is insecure. I also want to ask this question, such Dao and service classes will not be modified once they are created (but they cannot be declared as final because they need to be retrieved from spring ). Can it be understood as a tool class that provides some static methods (such as saveworld?

 

 

 

 

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.