Struts2 and OGNL expressions. The value of ognl in valuestack is used in jsp.

Source: Internet
Author: User

In Struts2, before a request reaches the Action method, the Action object itself will be pushed into ValueStack (actually put in the CompoundRoot of ValueStack). Therefore, the Action object is an element in CompoundRoot. See the following code:

Public class UserAction {

Private String username;

Private Integer age;

Private boolean valid;

// View User details

Public String detail (){

Username = "zhangsan ";

Age = 18;

Valid = true;

Return "detail ";

}

In the Action, assign values to the username/age/valid of the Action. The Detail page is as follows:

Username:

Valid:

Age:

The preceding JSP page correctly extracts their values. . The OGNL expression in the s: property tag will be handed over to ValueStack for explanation. Username is an OGNL expression that calls the getUsername () method of the root object. Struts2 will automatically search for elements in CompoundRoot (from 0th elements) and check whether these elements have the getUsername () method. If the 0th elements do not have the getUsername () method, you will continue searching for 1st, 2, 3 ...... Whether the getUsername () method exists for each element.

In the preceding example, only one object in CompoundRoot is the userAction object, and the getUsername () method is good in this object. Therefore, the preceding JSP code can correctly retrieve the value.

Let's look at the following example:

Public class UserAction {

Private String username;

Private String name;

// View User details

Public String detail (){

Username = "zhangsan ";

Name = "Wang Wu ";

User u = new User ();

U. setUsername ("Zhao Yi ");

ActionContext. getContext (). getValueStack (). push (u );

Return "detail ";

}

In the above UserAction code, we call ActionContext directly. getContext (). getValueStack (). the push () method directly pushes a User object (this object has the getUsername () and setUsername () Methods) to ValueStack. At this time, there will be two elements in CompoundRoot of ValueStack: the first element is the user object just pushed in [Zhao Yi], and the second element is the userAction object [Zhang San]. If the following expression is used in JSP to set the value:

The output value will be "Zhao Yi "! As mentioned above, struts2 will start searching for objects in CompoundRoot from 0th elements. The first element is the user object just pushed in!

If you use The "Wang Wu" will be taken out. Because the user object of The 0th elements does not have the name attribute, the userAction object of the 1st elements will continue to be searched. This object has the name attribute!


Let's look at the following code:

Public class UserAction {

Private String username;

// View User details

Public String detail (){

Username = "zhangsan ";

List list = new ArrayList ();

For (int I = 0; I <10; I ++ ){

User user = new User ();

User. setUsername ("User" + I );

List. add (user );

}

ActionContext. getContext (). put ("users", list );

User u = new User ();

U. setUsername ("Zhao Yi ");

ActionContext. getContext (). getValueStack (). push (u );

Return "detail ";

}

The corresponding JSP is as follows:

1:

2:

3:

4:

5:

6:

7:

According to the example just now, we know that the username in line 1 is "Zhao Yi" (because when JSP executes this line of code, CompoundRoot has two elements: 1st are "user object Zhao Yi ", 1st are "userAction object Michael"). Therefore, the username attribute of row 1st will be taken out of the username attribute of the 0th elements in CompoundRoot: Zhao Yi

The 2nd line of code is the iterator label that defines only one value attribute. The iterator label cyclically accesses the User object in the users List and pushes the user object in the current loop to CompoundRoot! Therefore, when lines 3rd and 4th of code are executed, CompoundRoot has three elements in total: 0th elements are the user objects of the current loop pushed by the iterator label; the 1st elements are "user object Zhao Yi", and the 2nd elements are "userAction object Zhang San". Therefore, the execution result of the 3rd-line code is to output "UserX ", that is, the username attribute of the user object in the current loop! The iterator label extracts the user objects in the List in sequence, and continuously pushes or pops up the user objects (the user objects will be pushed in or popped up once every cycle ). The code in the second line obtains the username attribute of the second element, that is, the username attribute of the userAction object: John.

After the execution of the 5th-line code is complete, there will be 2 elements left in CompoundRoot, the same as before the execution of the 2nd-line code. Therefore, the output of the 6th-line code is the same as that of the 1st-line code. The 7th-line code extracts the username attribute of the userAction object: Zhang San


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.