Inherit the ActionSupport class in struts

Source: Internet
Author: User

Theoretically, the Action of Struts 2.0 does not need to implement any interface or inherit any type. However, to facilitate the implementation of the Action, the Action will inherit from com. opensymphony. xwork2.ActionSupport class, And reload the String execute () method in this class (Override.

It can be seen from JavaDoc that the ActionSupport class implements interfaces.

Com. opensymphony. xwork2.Action.

Com. opensymphony. xwork2.LoaleProvider.

Com. opensymphony. xwork2.TextProvider.

Com. opensymphony. xwork2.Validateable.

Com. opensymphony. xwork2.ValidationAware.

Com. uwyn. rife. continuations. ContinuableObject.

Java. io. Searializable.

Java. lang. Cloneable.

As shown in the following example:

 
 
  1. public class HelloWorld extends ActionSupport {
  2. private String message;
  3. public String getMessage() {
  4. return message;
  5. }
  6. public String execute() {
  7. message = " Hello World, Now is " +
  8. DateFormat.getInstance().format( new Date());
  9. return SUCCESS;
  10. }
  11. }

You can configure the following mappings:

 
 
  1. <package name ="ActionDemo" extends ="struts-default">
  2. <action name ="HelloWorld" class ="tutorial.HelloWorld">
  3. <result> /HelloWorld.jsp</result>
  4. </action>
  5. </package>

By default, when HelloWorld is requested. when do occurs, Struts 2 will follow the struts. the Action ing set in xml is instantiated tutoiral. helloWorld class, and calls its execute () method. Of course, we can change this default call using the following two methods. This function (Feature) is somewhat similar to LookupDispathAction in Struts 1.

Create an Action in sturts. xml and specify the method to call it.

Add "! Xxx "(xxx is the method name ).

For example, you can add the following methods to tutorial/helloworld. Java:

 
 
  1. Public String aliasaction (){
  2. Message = "Custom Action call method ";
  3. Return success;
  4. }

Then you can specify the method in sturts. XML to set the function name of the request:

 
 
  1. <action name="AliasHelloWorld" class="
  2. tutorial.HelloWorld" method="aliasAction">
  3. <result>/HelloWorld.jsp</result>
  4. </action>

You can also use "! Method To set the function name of the request:

 
 
  1. http://localhost:8080/Struts 2Test
  2. /HelloWorld!aliasAction.action

Through the above two examples, careful friends may find that the action method (execute and aliasaction) in classes/tutorial/helloworld. Java returns success. This property variable is not defined by the author, so you should guess that it is defined in actionsupport or its parent class. Yes, success is defined in the interface com. opensymphony. xwork2.action. In addition, it also defines error, input, login, and none.

In addition, I didn't define the name (name) for the result when configuring the action, so they are all success by default. It is worth mentioning that the result in struts 2.0 is not just struts 1. the forward alias in X, which can implement exciting functions except forward, such as outputting actions to freemaker templates, velocity templates, jasperreports, and using XSL transformations. These are all defined by the Type attribute in result. You can also customize the result type.

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.