Struts2 framework study Note 5: OGNL expression, struts2ognl

Source: Internet
Author: User

Struts2 framework study Note 5: OGNL expression, struts2ognl

The value range of OGNL is divided into two parts: root and Context.

Any object can be placed as ROOT. The CONTEXT must be a Map key-value pair.

Example:

Preparations:

Public void fun1 () throws Exception {// prepare ONGLContext // prepare Root User rootUser = new User ("tom", 18); // prepare Context Map <String, user> context = new HashMap <String, User> (); context. put ("user1", new User ("jack", 18); context. put ("user2", new User ("rose", 22); OgnlContext oc = new OgnlContext (); // use rootUser as the root part oc. setRoot (rootUser); // use the Map of context as the oc part of Context. setValues (context); // write OGNL Ognl. getValue ("", oc, oc. getRoot (); // write the OGNL expression in}

User class:

package bean;public class User {    private String name;    private Integer age;                public User() {        super();        // TODO Auto-generated constructor stub    }    public User(String name, Integer age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    }
View Code

 

Syntax:

Retrieve objects from root:

// Retrieve the name attribute String name = (String) Ognl of the user object in the root user. getValue ("name", oc, oc. getRoot (); Integer age = (Integer) Ognl. getValue ("age", oc, oc. getRoot ());

 

Retrieve objects from Context:

// Retrieve the name attribute String name1 = (String) Ognl of the user1 object in the context. getValue ("# user1.name", oc, oc. getRoot (); String name2 = (String) Ognl. getValue ("# user2.name", oc, oc. getRoot (); Integer age = (Integer) Ognl. getValue ("# user2.age", oc, oc. getRoot ());

 

Assign values to attributes:

// Assign the name attribute of the user object in root to Ognl. getValue ("name = 'Jerry '", oc, oc. getRoot ());

Assign a value to context:

String name2 = (String) Ognl. getValue ("# user1.name = 'zhangsan'", oc, oc. getRoot ());

 

Method of calling the object:

// Call the setName method Ognl of the user object in root. getValue ("setName ('zhang san')", oc, oc. getRoot (); String name1 = (String) Ognl. getValue ("getName ()", oc, oc. getRoot (); String name2 = (String) Ognl. getValue ("# user1.setName ('Lucy '), # user1.getName ()", oc, oc. getRoot ());

(Note: The two statements can be written together and separated by commas. the return value is the last statement)

 

Call static methods:

        Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc,oc.getRoot());

 

Creation object:

// Create the list object Integer size = (Integer) Ognl. getValue ("{'Tom ', 'Jerry', 'jack', 'Rose '}. size () ", oc, oc. getRoot (); String name1 = (String) Ognl. getValue ("{'Tom ', 'Jerry', 'jack', 'Rose '} [0]", oc, oc. getRoot (); String name2 = (String) Ognl. getValue ("{'Tom ', 'Jerry', 'jack', 'Rose '}. get (1) ", oc, oc. getRoot (); // create the Map object Integer size2 = (Integer) Ognl. getValue ("# {'name': 'Tom ', 'age': 18 }. size () ", oc, oc. getRoot (); String name3 = (String) Ognl. getValue ("# {'name': 'Tom ', 'age': 18} ['name']", oc, oc. getRoot (); Integer age = (Integer) Ognl. getValue ("# {'name': 'Tom ', 'age': 18 }. get ('age') ", oc, oc. getRoot ());

 

 

How OGNL expressions work with Struts2:

OGNLContext is essentially the value stack in Struts2 (ValueStack)

ValueStack has two parts: ROOT and CONTEXT.

(Queue: FIFO, Stack: FIFO)

The ROOT part is a stack, and the CONTEXT part is ActionContext.

The stack in ROOT is essentially a container (SET)

For example, use the list set to create a stack structure container: push the stack method list. add (0, a); pop stack method list. remove (0), you can simulate a stack

 

There is a class in Struts2: The CompoundRoot class is a stack, and the implementation principle is similar to the above

Reason for using the stack: When the stack attribute is retrieved, the attribute will be searched from the top of the stack, and the attribute will not be found. Continue to find and stop.

By default, the stack contains the currently accessed Action object..

 

 

The combination of OGNL expressions and Struts2:

Struts2 has an interceptor params, which is used to pass the obtained parameters to OGNL for processing.

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.