Interpretation of two similar classes in Struts

zhaozj2021-02-12  145

Interpretation of two similar classes in Struts

Dispatchaction

=====================================================================================================================================================

I think Dispatchaction can be available, but Dispatchction does provide some convenience.

Summary of the usage of it is some instructions:

Dispatchaction's role is simple to put the original operation we write in multiple Actons in the same one.

Processing in Action.

For example, if there is a management operation in your system, then there is usually

The following: Add articles, check articles, search for articles, etc., this is usually written

Three action [artilcesaveaction articles] each handles each action,

Although it is very clear and smooth, you will find too much thing in three anctions.

Now use Dispatchaction, we can put the "similar" Action in an action.

The three anctions above the following are in an example:

IMPORT ****;

Import org.apache.struts.Actions.dispatchaction;

Public class articleaction extends dispatchaction {

/ **

* Aritcleaddaction

* /

Public ActionForward Add (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws exception {

...

...

}

/ **

* AritcleViewAction

* /

Public anctionForward View (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws exception {

...

...

}

/ **

* AritCleaserChaction

* /

Public ActionForward Search (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws exception {

...

...

}

}

Ok, the framework of this action has been completed, but if you want to use, there must be an indispensable operation,

That is to change your action mapping, the example above, as follows:

INPUT = "/ article / ***. JSP"

Parameter = "Method"

Scope = "request"

TYPE = "com. ***. ArticleAction"

Validate = "false">

Seeing you will find that it and us usually write multiple one: "Parameter =" Method ", this is reasonable and very important:

DispatchAction will determine call add, view, or search according to the specific method value.

As of the following request from Client:

Article.do?method=add triggers the action of adding an article.

The above is just a simple example to analyze the Dispatchction, and the situation of the actual application may be more complicated.

LookUpdispatchaction

=========================

Org.apache.struts.Actions.lookupdispatchaction.java

From the name, we can also see that lookupdispatchaction is the subclass of Dispatchction. They have many similarities

The place.

Below is there an example of a brief description:

It is usually mainly used to "there is a plurality of submission buttons in a form, and these buttons have a common name", and the names of these buttons should correspond to the value of the parameter in the specific action maping. [This is an important point]

The following code is intercepted from Struts-Config.xml:

TYPE = "com. ****. EditArticleAction"

Name = "ATRICLEFORM"

Scope = "request"

Parameter = "Action">

The form part of a JSP page is given below

Then there will be the following pieces in the corresponding ApplicationResources.properties:

Button.view = view the article

Button.delete = delete the Atricle

Also in this time, there is an abstract method in the lookupdispatchaction:

/ **

* Provides the mapping from resource key to method name

*

* @ Return Resource Key / Method Name Map * /

Protected Abstract Map getKeyMethodMap ();

This method you should be implemented in EditAction, as follows:

protected map getKeyMethodMap () {

Map map = new hashmap ();

Map.put ("Button.View", "View");

Map.Put ("Button.Delete", "delete");

Return Map;

}

Ok, suppose you have the following methods in your EditArticleAction:

Public anctionForward View (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

THROWS IOException, servletexception {

// ......

// ......

Return mapping.findforward ("Success");

}

Public ActionForward Delete (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

THROWS IOException, servletexception {

// ......

// ......

Return mapping.findforward ("Success");

}

The following examples have several assuming requests:

http: //...... The page has two buttons at this time, buttons 1 "View the articles", ", buttons 2" delete the ATRICLE "

Adjust the view method in the editArticle during the submission button 1;

The delete method in the EditAction;

There is still some explanation below;

If I have a button to depart a AA method, but there is no AA method in this action, and it will throw an exception; if there are two AA methods in the action, the first one is called.

Reference Resources: [Programming Jakarta Struts]

[Struts source package lookdispatchaction.java dispatchaction.java]

转载请注明原文地址:https://www.9cbs.com/read-6094.html

New Post(0)