Struts2 Action interface and ActionSupport class

Source: Internet
Author: User

In the Struts2 framework, you need to use execute in the response class to return strings in a way that corresponds to the name of the result component registered in the struts. xml file.


Returned string in execute:

1. the value in the returned string must match the name of the expected result configured in the declarative architecture.

Return "SUCCESS"
Corresponding:
/Helloworld. jsp

2. The struts2 Action does not need to implement the Action interface. Any object can implement the contract between the Action and the framework by implementing the execute () method of the return control string.

3. But the framework itself provides the Action interface and an implementation class. Why? Let's take a look at the Action interface: (See strtus2 api)


Action interface:

There is only one method: execute ()
And some useful String constants


E. g

 
  /NameCollector.jsp
 


The handler class is not specified here because there is no content to process. The Struts2 smart default value provides the default inherited Action implementation. This default Action has an empty execute () method and automatically returns the SUCCESS constant of the Action interface as the control string. Nothing else will be done.

Note: Generally, the Action interface is not directly implemented, because there is an implementation that can be borrowed-ActionSupport class


ActionSupport class
It implements the Action interface and provides functions such as data verification and error message localization.

1. Basic Verification
Prerequisites: the struts-default package must be extended to inherit the default interceptor stack.

The validate () method is provided to check the validity logic of the data received by the JavaBean attribute. If the verification fails, the workflow interceptor automatically redirects it to the result page named "input.

ActionSupport implements the ValidationAware interface and provides the following two methods for verification:

AddFieldError (String fieldName, String errorMessage)
AddActionError (String errorMessage)

In this way, we can clearlyVerification logic and business logicSeparated. Cause: the workflow interceptor controls the execution of the verification logic.

E. g

public class CheckDB extends ActionSupport{execute(){       //business logicreturn SUCCESS;}validate(){       //validate logic}}

The registration content in the struts. xml file should be Name is "input"Result:

  
 
  /dbresult.jsp
  
 
  /namequery.jsp
  

2. Use a resource package to process text messages

AddFieldError ("name", "Name is required! ");

Passing in strings will cause great inconvenience to future maintenance, especially internationalization. A widely accepted event is to put these messages in an externally maintainable resource package, which is usually implemented using simple attribute files.

ActionSupport implements two interfaces, which provide the localized messaging text function.
TextProvider Interface, Provides access to these messages.

E. g Unknown. properties

Name. required = Username is required.

In order for ActionSupport implementing the TextProvider interface to find this property file, we only need Put it in the same java package that contains the handler class.
After the property file is ready, you can use a getText () method provided by TextProvider to retrieve our message.

The above code can be changed:
AddFieldError ("name", getText (name. required ));

This layer makes message files more manageable. Changing a message means editing only the attribute file. Meaningful keywords in the source code never need to be changed.

LocaleProvider InterfaceProvides a basic international solution for localized message texts.
This interface only provides one method getLocale ()
Obtain the region of the user based on the region settings sent by the browser.

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.