#,%, and $ symbols in Struts2 OGNL usage notes

Source: Internet
Author: User
Tags min

#,%, and $ symbols are often present in OGNL expressions, and these three symbols are also part of the developer's difficult to grasp and understand. Here the author briefly describes their corresponding uses.

1. #符号的用途一般有三种.
1) Access to non-root object properties, such as the #session.msg expression in the example, 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").
2) used for filtering and projection (projecting) collections, such as persons in the example. {? #this. age>20}.
3) used to construct a map, for example, #{' foo1 ': ' bar1 ', ' foo2 ': ' Bar2 '}.

2. % symbol
The purpose of the% symbol is to evaluate the value of the OGNL expression when the property of the flag is a string type. As shown in the following code:
Construct map
<s:set name= "Foobar" value= "#{' foo1 ': ' Bar ', ' foo2 ': ' Bar2 '}"/>

<p>the value of key "Foo1" is <s:property value= "#foobar [' foo1 ']"/></p>

<p> do not use%:<s:url value= "#foobar [' foo1 ']"/></p>

<p> using%:<s:url value= "%{#foobar [' Foo1 ']}"/></p>

3. $ symbol
The $ symbol has two main uses.

In an internationalized resource file, reference the OGNL expression, such as the code in an 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, as shown in the following code snippet:
<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 between ${min} and ${max}. </message>
</field-validator>
</field>
</validators>

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.