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
- <s:set name= "Foobar" value= "#{' foo1′: ' bar1′, ' 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> Use%:<s:url value= "%{#foobar [' Foo1 ']}" /></P>
- <s:set name= "Foobar" value= "#{' foo1′: ' bar1′, ' 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> 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:
- <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} to ${max}! </message>
- </field-validator>
- </field>
- </validators>
- <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} to ${max}! </message>
- </field-validator>
- </field>
- </validators>
The difference between an El expression and an ognl expression (RPM)