The difference between an El expression and an ognl expression (RPM)

Source: Internet
Author: User

El expression:
>> appears only in JSP pages, is the value in four scopes, page,request,session,application.
>> if it is in a struts environment, it can take values from the value stack (valuestack) In addition to the value function in the above four scopes.
>> features 1:${name},name in the value stack, the order is: First take from the object stack, take to terminate, otherwise, to map.
>> feature 2: In the object stack to find the order is, first from the model to find whether the name of the property, find the termination, otherwise, find the action in the name of the global variable.
>> features 3:${#name}, the inside is not with the # number.
>> 4: If the object stack is placed in a custom object, then ${property} inside can go directly to the object's property values, not so ${object.property}


OGNL expression:
1: Read values passed from the background
%{#name}: Represents a value from a map in the value stack
%{name}: Represents a value from the stack of values in the value stack
%{#request. Name}: Indicates a value from the request domain

2: Build Your own data
A, build map<s:iterator var= "map" value= "#{' key1 ': ' value1 ', ' key2 ': ' value2 '}"/>
B, build list<s:iterator var= "list" value= "{' One ', '" ' ", ' Three '}" >

1. Differences

Usage Differences: OGNL is commonly used in conjunction with Struts 2 flags, such as <s:property value= "#xx"/> Struts page cannot be used alone, El can be used alone ${sessionscope.username}

Page value differences:

Name

Servlet

Ognl El

Parameters

Request.getparameter ("username")

#username ${username}

Request

Request.getattribute ("UserName")

#request. UserName ${requestscope.username}

Session

Session.getattribute ("UserName")

#session. UserName ${sessionscope.username}

Application

Application.getattribute ("UserName")

#application. UserName ${applicationscope.username}

attr

Used to access its properties by request > Session > Application Order (attribute)

#attr. Username is equivalent to reading the Username property sequentially within the three ranges (scope) until it finds the

2.OGNL explanation

OGNL is Struts 2 The default expression language. is an abbreviation for the object Graphic Navigation Language (objects Graph navigation language), which is an open source project.

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. The purpose of the% sign% 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

  1. <s:set name= "Foobar" value= "#{' foo1′: ' bar1′, ' foo2′: ' bar2′} ' />
  2. <p>the value of key "Foo1″is <s:property value=" #foobar [' foo1 '] " /></p >
  3. <P> do not use%:<s:url value= "#foobar [' foo1 ']" /></P>
  4. <p> Use%:<s:url value= "%{#foobar [' Foo1 ']}" /></P>
  1. <s:set name= "Foobar" value= "#{' foo1′: ' bar1′, ' foo2′: ' bar2′} ' />
  2. <p>the value of key "Foo1″is <s:property value=" #foobar [' foo1 '] " /></p >
  3. <P> do not use%:<s:url value= "#foobar [' foo1 ']" /></P>
  4. <p> Use%:<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:

  1. <validators>
  2. <field name= "Intb">
  3. <field-validator type= "int">
  4. <param name= "min">10</param>
  5. <param name= "max">100</param>
  6. <message>baction-test Checksum: The number must be ${min} to ${max}! </message>
  7. </field-validator>
  8. </field>
  9. </validators>
    1. <validators>
    2. <field name= "Intb">
    3. <field-validator type= "int">
    4. <param name= "min">10</param>
    5. <param name= "max">100</param>
    6. <message>baction-test Checksum: The number must be ${min} to ${max}! </message>
    7. </field-validator>
    8. </field>
    9. </validators>

The difference between an El expression and an ognl expression (RPM)

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.