Introduction to the basic use of ONGL expressions in the Java Struts framework

  • 2020-04-01 04:28:08
  • OfStack

First of all, let's understand the concept of OGNL:
OGNL is the abbreviation of object-graph Navigation Language, fully known as the Object map Navigation Language, is a powerful expression Language, it through simple and consistent syntax, you can arbitrarily access the properties of the Object or call the methods of the Object, can traverse the entire Object structure diagram, achieve Object property type conversion and other functions.

In addition, you need to know something about OGNL:
1.OGNL expressions are evaluated around the OGNL context.
An OGNL context is really just a Map object, represented by the ognl.ognlcontext class. It can hold many JavaBean objects. It has a context root object.
The root object in the context can access its property value directly using its name or directly using its property name. Otherwise, prefix it with "#key."

2. All tag libraries in Struts2 use OGNL expressions to access object data in ActionContext. Such as: < S: propertyvalue = "XXX" / > .

3.Struts2 sets the ActionContext to the OGNL context and places the value stack into the ActionContext as the root object of OGNL.

4. ValueStack (ValueStack) :
You can put, delete, and query objects in the value stack. Accessing objects in the value stack does not use "#".
Struts2 always places the current Action instance at the top of the stack. So you can also omit the "#" in OGNL when referencing an Action property.

5. Call ActionContext put(key,value) to put the data, need to use # access.


Three important symbols in OGNL: #, %, $:
#, %, and $are common in OGNL expressions, and these three symbols are not easy for developers to grasp and understand, it takes time to accumulate gradually clear...
1. The # symbol
There are three general USES of the # symbol.

Accessing non-root object properties, such as the #session.msg expression, requires a # prefix when accessing other non-root objects because the Struts 2 median stack is treated as a root object. In fact, # is equivalent to actioncontext.getcontext (); The #session.msg expression is equivalent to actioncontext.getcontext ().getsession ().getattribute (" MSG ").

Used for filtering and projection (projecting) collection, such as persons. {& # 63; # this. Age> }, 25 persons. {& # 63; # this. Name = = 'pla1}. {age} [0].

Used to construct maps, such as #{'foo1':'bar1', 'foo2':'bar2'} in the example.

2. The % symbol
The purpose of the % symbol is to evaluate the OGNL expression when the attribute of the token is of string type, which is similar to the eval in js and is very violent.
3. The $sign
The $sign has two main USES.

In the internationalization resource file, reference the OGNL expression, such as the code in the internationalization resource file: reg.agerange= internationalization resource information: age must be between ${min} and ${Max}.

Refer to the OGNL expression in the Struts 2 framework configuration file, for example:


<validators>  
  <field name="intb">  
      <field-validator type="int">  
      <param name="min">10</param>  
      <param name="max">100</param>  
      <message>BAction-test Check: the number must be ${min} for ${max} Between! </message>  
    </field-validator>  
  </field>  
</validators> 

 

Example: the first OGNL program


public class OGNL1 
{ 
  public static void main(String[] args) 
  { 
     
    Person person = new Person(); 
    person.setName("zhangsan"); 
     
    try 
    { 
       
      Object value = Ognl.getValue("name", person); 
 
      System.out.println(value); 
    } 
    catch (OgnlException e) 
    { 
      e.printStackTrace(); 
    } 
  } 
} 
 
class Person 
{ 
  private String name; 
 
  public String getName() 
  { 
    return name; 
  } 
 
  public void setName(String name) 
  { 
    this.name = name; 
  } 
} 

Console output:


zhangsan

You can see that we correctly get the name attribute value of the person object, and the getValue declaration is as follows:


public static <T> T getValue(String expression,Object root)throws OgnlException 
 
Convenience method that combines calls to parseExpression and getValue.  
 
Parameters: 
expression - the OGNL expression to be parsed 
root - the root object for the OGNL expression  
Returns: 
the result of evaluating the expression 

OGNL extracts values from the root object based on an expression.


Related articles: