Struts 2 (1): Conveyor mechanism for Valuestack objects

Source: Internet
Author: User
Tags type null

Source code and JAR package download (change rar into a jar, directly in the Web_inf\lib directory can be)

As we all know, the action class of strut 2 can get all the relevant values through attributes, such as request parameters, action configuration parameters, passing property values to other action (through chain results), and so on. The only thing we have to do to get these parameter values is to declare an attribute with the same name as the parameter in the action class, and then assign the value to the Action property before Struts 2 calls the action method of the Act class (the default is the Execute method).

To do this, Struts 2 relies heavily on the Valuestack object. This object runs through the life cycle of the entire action (the object instance of each action class will have a Valuestack object). When struts 2 when a request for an. Action is received, the object instance of the action class is first established, but the action method is not invoked, but the corresponding property of the action class is first placed on the top-level node of the Valuestack object (the Valuestack object is equivalent to a stack). Just all property values are default values, such as the property value of string type Null,int is 0.

When the above work is done, Struts 2 calls the interceptor in the interceptor chain, and when all the interceptors are called, the action method of the Operation class is finally invoked, before the action method is invoked. The property value in the top-level node of the Valuestack object is assigned to the corresponding property in the action class. It's important to note that we have a lot of flexibility here. That is, in the process of calling the interceptor in Struts 2, you can change the value of the property in the Valuestack object, and when a property value is changed, the corresponding property value of the action class becomes the value that finally changes the property in the Interceptor.

It is easy to know from the above description that the action class in Struts 2 can obtain a parameter value that has the same name as a property that is handled by a different interceptor, such as the interceptor that obtains the request parameter is params, and the interceptor that obtains the action configuration parameter is staticparams. Read the corresponding values inside these interceptors and update the values of the corresponding properties of the top-level nodes of the Valuestack object. The Valuestack object, like a conveyor belt, passes a property value from one interceptor to another (of course, in which the value of the property may change), and finally to the action object, assigning the end value of the property in the Valuestack object to the corresponding property of the action class.

Perhaps some readers will see a problem, if multiple interceptors change the same attribute value, then the interceptor referenced in the following will overwrite the value of the previously referenced interceptor changed property. Because Staticparams is referenced before params in the Defaultstack interceptor stack, the request parameter value overrides the configuration parameter value if a request parameter has the same name as the configuration parameter of the action class.

Here we use an example to illustrate this process. In this example, an interceptor is implemented, and the function of the interceptor is to map the Key-value in a property file to the value of the corresponding property. As the following is the contents of a property file:

name = 超人
price = 10000

We can define the name and price attributes in the action class, and when this interceptor is referenced in the action, the property is automatically assigned a value.

The following rules are used for using the Interceptor:

1. The property file path that the interceptor reads is specified by the path parameter.

2. The encoding format of the property file is specified by the encoding parameter, and the default value is UTF-8.

3. If a key contains a "." (the symbol cannot appear in the identifier), you have the following processing method:

(1) Define the property name of the action class to remove the "." The key. For example, key is Person.name, and the property name can be defined as personname.

(2) Define the property name of the action class as the "." A symbol that is replaced with another character. For example, the key is Person.name, and the property name can be defined as Person_name, where "_" is specified by the separator parameter.

4. If the key is too long, you can also use the action parameter to map directly, for example, key is Country.person.name, you can do the following mapping:

<param name= "Countrypersonname" >name</param>

Note that the Name property value cannot contain ".", so the "." In the key value should be Remove. You can now define a property named name in the action class directly, and the value of the Name property will be the same as the key value.

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.