Struts2 data presentation

Source: Internet
Author: User

Struts2 data presentation
Struts2 data presentationI. OGNL

What is GONL?

Object Graph Navigation Language is a powerful expression Language, similar to EL.
Struts2 uses the OGNL expression to access Action data by default, and uses the encapsulated OGNL to access Action through ValueStack.
OGNL is an independent open-source component. Struts2 has transformed and encapsulated it. To understand the operating principle of OGNL in Struts2, please refer to ValueStack.

Struts2 display tag

In Struts2, The OGNL expression must be used with the Struts2 tag to access data. That is, the OGNL expression must be written in the Struts2 tag. Therefore, we will first introduce the first Struts2 tag-display tag.

The tag is used to access the Action according to the OGNL expression and replace the retrieved data with the tag itself.

Two common OGNL expressions and six uncommon GONL expressions

There are eight methods to use OGNL expressions, the first two of which are frequently used, and the last six can be understood.
1. access basic attributes
Syntax:
Explanation: This method directly displays the basic attributes in the Action to the position of the tag. The attribute name refers to the attributes in the Action.
Example:

2. Access Object
Syntax:
Explanation: This method is used to display the object attributes in the Action to the tag position. The object name refers to the object in the Action, and the attribute name refers to the attributes in the object.
Example:

We can see that the two methods to access Action are actually exactly the same as the EL expression usage. They are the most common and easy to master methods.

3. Access an array in Action or an element in a set. The attribute refers to the array or set attribute in Action, and the index refers to the subscript of an array or set.

4. Access the Map type attribute value in the Action, where the attribute refers to the Map type attribute in the Action, the KEY refers to the key of the Map attribute, and the corresponding value is output through the key.

5. After accessing the Action data, you can directly compute the results returned by the OGNL expression and output the results to the tag location.

6. After accessing the Action data, you can directly call the method returned by the OGNL expression and output the final result to the tag location.

7. Instead of accessing the Action data, you can directly use the OGNL expression to create a temporary set and return the result to the tag location.

8. Instead of accessing the Action data, you can directly use the OGNL expression to create a temporary Map and return the output to the tag location.

Ii. ValueStack

What is ValueStack (value stack )?

ValueStack is the medium in Struts2 where Action transmits data to the page. ValueStack encapsulates the Action data and allows JSP to access it through OGNL.
Struts2 uses ValueStack to encapsulate the OGNL component, which is actually the transformed OGNL. We do not need to pay attention to the transformation process, you only need to understand the operating principle of the transformed OGNL in ValueStack.
ValueStack encapsulates the OGNL parsing engine to parse the passed OGNL expressions. The purpose is to access Java objects by TAG + string on the page, thus reducing the difficulty of Page code development, this improves the efficiency of Page code maintenance.


 

The OGNL engine can access two types of objects: Stack and Map.

1. Stack

By default, the OGNL expression accesses the stack. The access rule is to take each level of elements of the stack as the root object from the top to bottom of the stack and return the value until the value is obtained, if no data is obtained, null is returned.
The root object is a JavaBean object, which is called in ValueStack. OGNL can access the root object directly from the root object attribute. For example, if an Action is the root object and the user attribute in the Action is accessed, the OGNL expression can be written as user. xxx.

2. Map

If the OGNL expression starts with "#", the OGNL engine accesses objects of the Map type. In this case, the OGNL expression is written as # key and the returned value is the corresponding value of the key in the Map, this type of object is usually called the context object.
The context object is used to encapsulate context data of a program, including request, session, page, and application. In fact, the context object encapsulates complete data and contains action data.

Access ValueStack

The principle of ValueStack is abstract, and its structure is not intuitive enough. In this case, Struts2 provides a debugging tag To observe the ValueStack structure.

Iteration set

We can use the Struts2 iteration tag combined with OGNL to iterate the set attributes in the Action. The syntax of the iteration set tag is as follows:

Users is an OGNL expression. It accesses the users attribute of the root object in the ValueStack Stack from top to bottom. Here, the set attribute (List Users) value.

During the iteration process, the top of the ValueStack stack changes, and the cyclic variable User is pushed to the top of the stack. At this time, the Action is pushed to the second place of the stack, that is, the stack top changes from the original Action to the cyclic variable User. At the top of the stack, It is a cyclic variable.

Sum up experience

The ValueStack structure seems complicated, but what we need to focus on is nothing more than the changes on the top of the stack. This change only happens in a loop, so remember this unique change, that is

1. The stack top is Action by default.
2. During the loop process, the top of the stack is a cyclic variable.
3. After the loop ends, the stack top changes back to Action.

3. UI tag

Functions of UI labels

UI labels are used in many ways. Their core function is to generate form frames and assign default values to them. Therefore, they are often used for modification, but are not limited to modification.
To facilitate your understanding, we divide UI labels:
1. Simple UI tag
2. Complex UI labels

Simple UI tag

1. form elements used to generate HTML. theme is used to specify a topic. simple is a simple topic and does not contain styles or tables during creation.
2. This tag is used to generate a summit button.
3. First, it generates a text box, and then it accesses the ValueStack value based on the OGNL expression, and sets the obtained result to the default value of the text box.
4. The role of the password box is exactly the same as that in the text box. The difference is that it generates a password box.
5. The text field functions exactly the same as the text box. The difference is that it generates a text field.
6. Generate a checkbox. Next, it accesses the valueStack value based on the OGNL expression. The value here must be of the boolean type. Then, it sets whether to check the checkbox Based on the returned results, if the returned value is true, this option is selected. Otherwise, this option is not selected.
Complex UI labels

1. Single region
ListKey = "cityCode" listValue = "cityName"/>
1. Access ValueStack Based on the OGNL expression (cities). The accessed attribute should be set (List ), And generates a group of ticket tokens Based on the returned results. If there are several values in the Set, several radio values are generated. During this period, the radio value is generated based on the City attribute specified by listKey, and the display value label of radio is generated based on the City attribute specified by listValue.
2. Access ValueStack Based on the OGNL expression (favoriteCity) and compare the returned result with the value of radio. If the value of which radio returns the same result, this radio is selected by default.

Summary

The two usage methods differ in the first initialization option.
First, it is initialized to a fixed value.
The second method initializes dynamic values.
For the default option of setting a single region, the two are completely consistent.

2. Multiple selection box

List = "# {'01 ': 'beijing', '02': 'shanghai', '03': 'guangzhou ', '04': 'shenzhen '}"/>
1. Map (# {'01 ': 'beijing', '02': 'shanghai', '03': 'guangzhou', '04 'created based on the OGNL expression ': 'shenzhen '}) to generate a set of multiple selection boxes. If there are several key-value pairs in Map, several checkboxes are generated. The key of Map is used to generate the value of checkbox, and the value of Map is used to generate the label value of checkbox.
2. Access ValueStack Based on the OGNL expression (travelCities). The accessed attribute is set (List ), And compare the returned result with the value of the checkbox. If the value of the checkbox is in the set of returned results, the checkbox is selected by default.
ListKey = "cityCode" listValue = "cityName"/>
1. Access ValueStack Based on the OGNL expression (cities). The accessed attribute should be set (List ), And generate a set of multiple selection Boxes Based on the returned results. If there are several values in the Set, several checkboxes are generated. During this period, the checkbox value is generated based on the object (City) attribute specified by listKey, And the checkbox display value is generated based on the object (City) attribute specified by listValue.
2. Access ValueStack Based on the OGNL expression (travelCities). The accessed attribute is set (List ), And compare the returned result with the value of the checkbox. If the value of the checkbox is in the set of returned results, the checkbox is selected by default.

Iii. drop-down list

List = "# {'01 ': 'beijing', '02': 'shanghai', '03': 'guangzhou ', '04': 'shenzhen '}"/>

1. Map (# {'01 ': 'beijing', '02': 'shanghai', '03': 'guangzhou', '04 'created based on the OGNL expression ': 'shenzhen '}) to generate a set of multiple selection boxes. If there are several key-value pairs in Map, several checkboxes are generated. The key of Map is used to generate the value of checkbox, and the value of Map is used to generate the label value of checkbox.
2. Access ValueStack Based on the OGNL expression (home). The accessed attribute is set (List ), And compare the returned result with the value of the checkbox. If the value of the checkbox is in the set of returned results, the checkbox is selected by default.

ListKey = "cityCode" listValue = "cityName"/>
1. Access ValueStack Based on the OGNL expression (cities). The accessed attribute should be set (List And generate a group of drop-down lists based on the returned results. If there are several values in the Set, several options are generated. During this period, the value of option is generated based on the object (City) attribute specified by listKey, and the display value of option is generated based on the object (City) attribute specified by listValue.
2. Access ValueStack Based on the OGNL expression (home) and compare the returned result with the value of option. If the value of option is the same as that of the returned result, this option is selected by default.
 

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.