Struts1-learning notes-action

Source: Internet
Author: User
Document directory
  • 1. dispatchaction extends baseaction
  • 2. lookupdispatchaction extends dispatchaction
  • 5. includeaction extends baseaction

The following is a summary of some common actions in struts, such:

  1. Dispatchaction
  2. Lookupdispatchaction
  3. Mappingdispatchaction
  4. Forwardaction
  5. Includeaction

1. dispatchaction extends baseaction

For example, <action Path = "/createuser" type = "examples. useraction "> here, useraction only needs to inherit the parent class (extends action class), and then rewrite the execute method of the parent class to implement specific control steering in execute.

We need to distribute different actions for adding, modifying, and deleting the same formbean. There are two methods here.

One is to perform different redirection through the IF judgment in the execute method:

(I) In the execute METHOD OF THE useraction class

String method =Request. getparameter ("method ");

If (method. Equals ("CREATE ")){

......

Return Mapping. findforward ("createuser ");

}

If (method. Equals ("save ")){

......

Return Mapping. findforward ("saveuser ");

}

(Ii) struts-config.xml:

<Action Path = "/createuser" type = "examples. useraction"

Name = "userform"

Scope = "request">

<Forward name = "createuser" Path = "/pages/listuser. jsp"/>

</Action>

<Action Path = "/saveuser" type = "examples. useraction"

Name = "userform"

Scope = "request">

<Forward name = "saveuser" Path = "/pages/saveuser. jsp"/>

</Action>

(3) You can define a Hidden variable on the page to specify the corresponding operation.

// Do not use <HTML: hidden property ="Method"/>

// Because the corresponding property needs to be defined in formbean, we can use common hidden fields

<Input type = "hidden" name ="Method"/>

Then define a JavaScript function. When you click the submit button, modify its value in the function.

<SCRIPT>

Function set (operation ){

With (document. Forms [0]) {

Method. value = operation;

}

}

</SCRIPT>

When you click the submit button, set the submitted method property value through the set method:

<HTML: Submit onclick = "set ('create');"> Create

<HTML: Submit onclick = "set ('save');"> Save

The second is to make useraction inherit dispatchaction, And the execute method does not need to be rewritten:

Public actionforwardCreate(Actionmapping mapping,

Actionform form,

Httpservletrequest request,

Httpservletresponse response)

Throws exception {

//
Perform some create logic.

//......

Return Mapping. findforward ("createuser ");

}

Public actionforwardSave(Actionmapping mapping,

Actionform form,

Httpservletrequest request,

Httpservletresponse response)

Throws exception {

//
Perform some save logic.

//......

Return Mapping. findforward ("saveuser ");

}

(Ii) The dispatchaction configuration is slightly different from the general action, that is, you must add a parametr attribute in the action configuration. This attribute can specify the method for executing the dispatchaction.

Struts-config.xml in:

<Action Path ="/Processuser"Type =" examples. useraction"

Name = "userform"

Scope = "request"

Parameter="Method">

<Forward name = "createuser" Path = "/pages/listuser. jsp"/>

<Forward name = "saveuser" Path = "/pages/saveuser. jsp"/>

</Action>

(3) Here we specify the parameter valueMethodWhen the page is submitted, we must specify the method parameter of the action when the page is submitted to determine which action method we want to call.

<SCRIPT>

Function submitform (operation ){

With (document. Forms [0]) {

Action = Action + '? Method = '+ operation;

Submit ();

}

}

</SCRIPT>

When you click the submit button, you can use the submitform method to set the method parameter of the action when submitting:

<HTML: Form Action ="/Processuser"Method =" get ">

<HTML: button onclick = "submitform ('create');"> Create

<HTML: button onclick = "submitform ('save');"> Save

</Html: Form>

2. lookupdispatchaction extends dispatchaction

Lookupdispatchaction inherits the dispatchaction.(3) for multiple submit buttons on the same page, there is no need for so many complex JS functions to specify the method parameter of the submitted action, that is, the above submitform (Operation) the lookupdispatchaction is used as follows:

Use messageresource to associate the button text with the reskey, for example, button. Save = save;
(3) Replace lookupdispatchaction with the following:

<HTML: Form Action = "/processuser" method = "get">

<HTML: Submit property ="Method
">

<Bean: Message key = "button. Create
"/>

</Html: Submit>

<HTML: Submit property ="Method
">

<Bean: Message key = "button. Save
"/>

</Html: Submit>

</Html: Form>

In the action configuration, there is one more parametr attribute. The attribute value is the same as the property attribute value of the submit button. This attribute can be used to specify the method for executing the lookupdispatchaction.

Struts-config.xml in:

<Action Path ="/Processuser
"Type =" examples. useraction"

Name = "userform"

Scope = "request"

Parameter="Method
">

<Forward name = "createuser" Path = "/pages/listuser. jsp"/>

<Forward name = "saveuser" Path = "/pages/saveuser. jsp"/>

</Action>

Use useraction to inherit the lookupdispatchaction, override the getkeymethodmap () method, and match the reskey with the methodname, as shown below:

Protected map getkeymethodmap (){

Map map = new hashmap ();

Map. Put ("button. Create", "CREATE ");

Map. Put ("button. Save", "save ");

Return map;

}

Note:The dispatchaction class uses the value of the request parameter to determine which method to call. The lookupdispatchaction class uses the value of the request parameter to reverse query resource binding and matches it with a method in the class, in fact, these two methods are similar.

3. mappingdispatchaction extends dispatchaction

The parameter value specified by dispatchaction isMethodWhen the page is submitted, we must specify the method parameter of the action to be submitted to determine which action method we want to call, while mappingdispatchaction maps multiple action-mapping to different methods of the same action class through the struts-config.xml directly:

<Action Path = "/createuser" type = "examples. useraction"

Name = "userform"

Scope = "request"

Parameter = "CREATE">

<Forward name = "createuser" Path = "/pages/listuser. jsp"/>

</Action>

<Action Path = "/saveuser" type = "examples. useraction"

Name = "userform"

Scope = "request"

Parameter = "save">

<Forward name = "saveuser" Path = "/pages/saveuser. jsp"/>

</Action>

Then useraction inherits mappingdispatchaction:

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

Note:View the source code of mappingdispatchaction:

Protected string getmethodname (actionmapping mapping, actionform form,

Httpservletrequest request, httpservletresponse response,

String parameter) throws exception {

// Return the unresolved mapping parameter.

Return parameter;

}

You can see that the method it calls is to directly return the value of parameter in the struts-config.xml.

4. forwardaction extends baseaction

Equivalent to the <JSP: Forward> function, do not need to configure formbean and action, you can directly jump, just need to configure in the struts-config.xml:

<Action Path = "/listuser"

Type = "org. Apache. Struts. Actions. forwardaction"

Scope = "request"

Parameter = "/pages/listuser. jsp">

</Action>

The parameter attribute is used to specify the page to which the forward belongs. The path, type, and parameter attributes are required. Other attributes can be omitted.

5. includeaction extends baseaction

Equivalent to the <JSP: Include> function, which needs to be configured in the struts-config.xml:

<Action Path = "/listuser" type = "org. Apache. Struts. Actions. includeaction"

Name = "userform"

Scope = "request"

Parameter = "/pages/shortdepage. jsp">

</Action>

Related Article

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.