OGNL Expression of Struts2

Source: Internet
Author: User

Basic concepts:

OGNL (Object Graph Navigation Language) is an expression language. Using this expression language, you can access any attribute in the Java object Tree, invoke the Java object Tree method, and automatically implement the necessary type conversions, with some kind of expression syntax. If we think of an expression as a string with semantics, then OGNL is undoubtedly a bridge between this semantic string and the Java object.

The OGNL API appears to be two simple static methods:

Public Static Object GetValue (object tree, Map context, object root) throws Ognlexception;

public static void SetValue (object tree, Map context, object root, Object value) throws Ognlexception;

The calculation of the OGNL expression is done around the OGNL context. The OGNL context is actually a map object, by OGNL. The Ognlcontext class represents. 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 using the name or directly using its property name. Otherwise the prefix "#key" is added. in Struts2 the OGNL must be used with the Struts label and the El Expression can be used directly,Can use El must be able to use OGNL, can use OGNL not necessarily be able to use El 3 Important symbols in OGNL: #,%, $: 1. #符号

#符号的用途一般有三种.

-Access to non-root object properties, such as the #session.msg expression, because the value stack in Struts 2 is considered a root object, the # prefix is required to access other non-root objects. In fact, #相当于ActionContext. GetContext (); #session. MSG expression is equivalent to Actioncontext.getcontext (). GetSession (). GetAttribute ("MSG").

-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. % symbolThe purpose of the% symbol is to calculate 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. $ symbolthe $ symbol has two main uses. -In an internationalized resource file, refer to the OGNL expression, such as the code in the internationalized resource file: reg.agerange= internationalized Resource information: Age must be between ${min} and ${max}. -Reference the OGNL expression in the configuration file of the Struts 2 framework, for example
1.<Validators>2.<FieldName= "INTB">3.<Field-validatorType= "int">4.<ParamName= "Min">10</Param>5.<ParamName= "Max">100</param> 6. <message>baction-test Checksum: The number must be ${min} to ${max}! </Message> 7. </field-validator> 8. </Field> 9.  </validators>             
use of OGNL expressions

1. Use the OGNL expression for the stack of objects in the value stack:

Read the properties in the object stack:
Object.propertyname generally use this
object[' PropertyName ']
object["PropertyName"]
Objects in ①, Objectstack can be referenced by a zero-based subscript.
The top object in ② and Objectstack can be referenced by [0], and the object below it can be referenced by [1].
③, if you want to return the value of the message property for the top object of the stack: [0].message or [0]["message"] or [0][' message ']
④, if the specified attribute is not found in the specified object, the search continues to the next object in the specified object. The meaning of [n] is to start with the nth search, not just the Nth object
⑤, if you start the search from the top of the stack, you can omit the subscript section
Combine S:property Tags: <s:property value= "message" > or <s:property value= "[0].message" >

2. Use the OGNL expression (parameters,request,session,application,attr) for the map stack in the value stack:

Get the properties for the map stack: <s:property value= "#XXX. Username"/>

3. Control OGNL expression parsing or non-parsing

  

<!---            <   name = "Telnum" Value = "username" Scope = "Request" ></ S:set >< BR >

Just change the value= "username" to value= "' username '"

<!-- when necessary, OGNL forced parsing%{}: Forced parsing  -            <  href= "Login.action?name=%{name}">${name}</s:a >

A string can be forced to be parsed using%{}

  

OGNL Expression of Struts2

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.