Getting started with the basics of Ongl expressions in the Java Struts Framework _java

Source: Internet
Author: User

First understand the concept of OGNL:
OGNL is the abbreviation of Object-graph Navigation language, the full name is a powerful expression language, it can access the object's properties or invoke the object's method arbitrarily through the simple and consistent syntax. Can traverse the entire object's structure diagram, realizes the object attribute type transformation and so on function.

In addition, we need to understand some of the knowledge of OGNL first:
The calculation of a 1.OGNL expression is done around the OGNL context.
The OGNL context is actually a map object, by OGNL. Ognlcontext class representation. It can store a lot of JavaBean objects. It has a context root object.
The root object in the context can access its property values directly by using a name or directly using its property name. Otherwise, prefix "#key" is added.

2.STRUTS2 's tag libraries use OGNL expressions to access object data in the Actioncontext. such as: <s:propertyvalue= "xxx"/>.

3.struts2 sets the Actioncontext to the OGNL context and places the value stack as the Ognl root object into the Actioncontext.

4. Value stack (valuestack):
You can put, delete, and query objects in the value stack. Access to objects in the value stack does not need "#".
Struts2 always places the current action instance on the top of the stack. So referencing the attributes in the action in OGNL can also omit "#".

5. Call Actioncontext put (Key,value) into the data that needs to be accessed using #.


important 3 symbols in OGNL: #,%, $:
#,%, and $ symbols are often present in OGNL expressions, and these three symbols are part of the developer's hard to understand and need to accumulate over time to figure out ...
1. #符号
#符号的用途一般有三种.

Access to non-root object properties, such as #session.msg expressions, which require a # prefix when accessing other non-root objects because the Struts 2 median stack is treated as the root object. In fact, #相当于ActionContext. GetContext () #session. MSG expression is the equivalent of Actioncontext.getcontext (). GetSession (). GetAttribute ("MSG").

Used for filtering and projection (projecting) collections, such as persons. {#this. age>25},persons. {#this. name== ' Pla1 '}. {Age} [0].

Used to construct a map, for example, #{' foo1 ': ' bar1 ', ' foo2 ': ' Bar2 '}.

2. % symbol
The purpose of the% symbol is to compute the value of the OGNL expression when the attribute of the flag is a string type, which is similar to the eval in JS, very violent.
3. $ symbol
the $ symbol has two main uses.

In internationalized resource files, reference OGNL expressions, such as code in internationalized resource files: reg.agerange= internationalized Resource information: Age must be between ${min} and ${max}.

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

<validators>  
  <field name= "INTB" >  
      <field-validator type= "int" >  
      <param name= "min" >10</param>  
      <param name= "Max" >100</param>  
      <message>baction-test Checksum: The number must be ${min } is between ${max}! </message>  
    </field-validator>  
  </field>  

Example: First OGNL program

public class OGNL1 
{public 
  static void Main (string[] args) 
  {/ 
    * Create a Person object */person person 
    = new P Erson (); 
    Person.setname ("Zhangsan"); 
     
    Try 
    { 
      */* Gets the value of the Name property from the Person object * 
      /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 have correctly obtained the name attribute value of the person object, which is declared as follows: GetValue

public static <T> T GetValue (String expression,object root) throws Ognlexception 
 
convenience method Combines calls to ParseExpression and GetValue.  
 
Parameters: 
expression-the OGNL expression to is parsed root-the the 
root object for the OGNL  
expression NS: The result of 
evaluating the expression 

OGNL extracts the value from the root object (root) based on an expression.

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.