Application: Valuestack

Source: Internet
Author: User

Understand the basic mechanism of Valuestack! Explain the various phenomena.

Valuestack is actually the encapsulation of OGNL, OGNL main function is to assign values and values, STRUTS2 is through the valuestack to the assignment and value!

Valuestack is an interface, and Ognlvaluestack is the default implementation in STRTUS2. The data in Valuestack is stored in two parts: root and Context (which is consistent with the concept in OGNL) while Valuestack exposes the associated interface:

void SetValue (String expr, Object value);

Object findvalue (String expr);

Used to manipulate data in valuestack with OGNL expressions!

The value stack is a list.

The root object in Valuestack is that Compoundroot,compoundroot inherits Arrarylist, providing additional methods: the Push () and Pop () methods, which are used to access the data contained in the root object!

Public class Compoundroot extends ArrayList {

Public Compoundroot () {

}

Public Compoundroot (List list) {

Super (list);

}

Public Compoundroot cutstack (int index) {

return New Compoundroot (sublist (index, size ()));

}

Public Object Peek () {

return get (0);

}

Public Object Pop () {

return Remove (0);

}

Public void push (Object o) {

Add (0, O);

}

}

It is through these two methods, Compoundroot becomes a stack structure! The stack operation will cause the object to be placed on the No. 0 element of the Compoundroot (the No. 0 element is the top of the stack), and the other objects are moved backwards, and the stack operation will cause the No. 0 element of the compoundroot to be removed (that is, the top element of the stack is ejected), and the other objects are moved forward sequentially!

OGNL does not support multiple root objects, and struts2 can support multiple root objects, which extends the OGNL.

If a OGNL expression is passed to Valuestack (that is, the SetValue or Findvalue method that calls Valuestack), and the expression contains access operations to the root object, Valuestack will search for objects contained in the Compoundroot object from the top of the stack, in turn, to see which objects have the corresponding properties, and return immediately after they are found.

In Struts2, theaction object itself is pressed into the Valuestack(which is actually placed in the compoundroot of Valuestack) before a request is finally reached to the action method, so The action object is an element in the Compoundroot . Look at the following code :

public   class  useraction {

     private  string username;

     private  integer age;

     private   boolean  valid;

   

    //View user's details

     public  string detail () {

       

       username =  "Zhang San";

       age =;

       valid =  true ;

      

        return   "detail";

    }

In action, assign a value to the username/age/valid of the action. The detail page is as follows:

Username:<s:property value= "username"/> <br/>

Valid:<s:property value= "valid"/> <br/>

Age:<s:property value= "Age"/> <br/>

The JSP pages above will be able to correctly remove their values. <s:property value= "ognl expression "/>. The OGNL expression in the s:property tag will eventually be given to Valuestack to explain. Username is a ognl expression, meaning the GetUserName () method that invokes the root object . Struts2 will automatically search for elements in the Compoundroot (starting with the No. 0 element), detect if the elements have a getusername () method, and if the No. 0 element does not have a getusername () method, it will continue to search for 1th, 2, 3 ... Element has a GetUserName () method.

In the above example, there is only one object in Compoundroot, which is the Useraction object, and this object has exactly the GetUserName () method, so the above JSP code will be able to remove the value correctly.

Let's look at the following example:

Public class useraction {

Private String username;

Private String name;

View user's Details

Public String Detail () {

Username = "Zhang San";

name = "Harry";

User U = new user ();

U.setusername ("Zhao Yi");

Actioncontext. GetContext (). Getvaluestack (). push (U);

return "Detail";

}

In the above useraction code, we call the Actioncontext.getcontext (). Getvaluestack (). Push () method to put a user object (this object has a getusername () and the Setusername () method) are pressed directly into the valuestack, this time, there will be two elements in the compoundroot of Valuestack: the No. 0 element is the user object just pressed in [Zhao Yi], The 1th element is the Useraction object [Zhang San], if the following expression is used in the JSP to take a value:

<s:property value= "username"/>, then the output value will be "Zhao Yi"! As already mentioned,Struts2 will search for objects in Compoundroot starting from the No. 0 element, and the No. 0 element is the user object just pressed in!

If you use <s:property value= "name"/> in the JSP to fetch the value, "Harry" is taken out because the No. 0 element of the user object does not have a Name property, so the 1th element Useraction object will continue to be searched, There is a name attribute in this object!

Then look at the following code:

Public class useraction {

Private String username;

View user's Details

Public String Detail () {

Username = "Zhang San";

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: <s:property value= "username"/> <br/>

2: <s:iterator value= "#users" >

3: <s:property value= "username"/>

4: <s:property value= "#root [2].username"/><br/>

5: </s:iterator>

6: <s:property value= "username"/>

7: <s:property value= "#root [1].username"/> <!--Zhang San--

According to the example above, we know that the username of line 1th is "Zhao Yi" (since the JSP executes this line of code, there are two elements in Compoundroot: the No. 0 is "user Object Zhao Yi", 1th is "Useraction object Zhang San"), So the username of line 1th takes out the username attribute of the No. 0 element in Compoundroot: Zhao Yi

The 2nd line of code is the iterator tag, which defines only one value property, and the iterator tag iterates through the user object in the Users list and presses the user object of the current loop into Compoundroot ! So, when lines 3rd and 4th are executed, there are a total of 3 elements in Compoundroot : the No. 0 element is the user object of the current loop that is pressed into the iterator tag, and the 1th element is "user Object Zhao Yi", and the 2nd element is " Useraction Object Zhang San ", so the 3rd line of code executes the output"UserX", the Username property of the user object of the current loop! The iterator tag will take out the user object in the list in turn and press/eject the user object continuously (each time the loop is pressed/popped). The 4th line of code takes the 2nd element's Username property, which is the Username property of the Useraction object: Zhang San.

After the 5th line of code executes, 2 elements will be left in Compoundroot as before the 2nd line of code is executed. So, the output of line 6th is the same as the 1th line of code, and the 7th line of code takes out the username property of the Useraction object: Zhang San

Application: Valuestack

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.