You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by José Cervera <jo...@gmail.com> on 2008/01/07 11:06:14 UTC

Expression languages

Hi,

I'm using Struts 2.0.11.

I'm a bit confused with the available "expression languages". I've found
that ${ ... }, %{ ... } and #... are all possible, but I'd like to know who
defines them, in which tags they are available, in which scopes they are
available, etc.

If I understand correctly, the ${ ... } is replaced by the servlet engine (
e.g. Tomcat), so if I include it in a struts Tag, it will be replaced by a
literal before it arrives to struts. Besides, this makes it available
outside a tag. For instance,

<html><body>
${myVar}
</body></html>

will display the contents of myVar if the server complies with JSP2.0.

Expressions with #... are OGNL, and are replaced by struts (webworks),
right?

What about the %{ ... } ?

Is there a document describing how all these options fit together?

Thanks in advance.

Re: Expression languages

Posted by "lei.java@gmail.com" <le...@gmail.com>.
>
> If I understand correctly, the ${ ... } is replaced by the servlet engine
> (
> e.g. Tomcat), so if I include it in a struts Tag, it will be replaced by a
> literal before it arrives to struts.


${...} is not allowed to be used in a struts tag. If you do it, you'll get a
syntax error.

Thanks.

Re: Expression languages

Posted by José Cervera <jo...@gmail.com>.
Well,

thanks a lot for such a complete answer. I'm sure it will be useful for
others... I guess it could even be included in the FAQ.

Best regards.

On Jan 7, 2008 2:00 PM, Jeromy Evans <je...@blueskyminds.com.au>
wrote:

> Hi José,
>
> [...]



>
> References:
> [JSP2.1] http://jcp.org/aboutJava/communityprocess/final/jsr245/index.html
> [JSP2.0] http://java.sun.com/products/jsp/docs.html
> [OGNL] http://struts.apache.org/2.x/docs/ognl.html
>
>
>
>

Re: Expression languages

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Hi José,

You're understanding is quite close.  Unfortunately I don't know of a 
document that describes how all the EL's fit together but here's a quick 
explanation (I'll skip the history though):

The Expression Language is part of the JSP specification and the version 
you're using depends on the container your using: Tomcat 5.5 is JSP 2.0. 
Tomcat 6 is JSP2.1.
The EL has also been separated from the JSP standard into the Unified 
Expression Language (Glassfish V2, JUEL).

You're correct it's evaluated by the container as each tag/page is 
rendered.  However it's important to note that tags can explicitly 
allow/disallow the processing of expressions in each attribute.

OGNL is a general-purpose object expression language.  Tags that extend 
an XWork Component in Struts 2 explicitly allow the attributes to be 
evaluated as OGNL expressions.  That is, the Struts2 tags are just 
normal JSP tags but explicitly include code that parses certain 
attributes as OGNL.  It depends on the tag whether an attribute is 
assumed to be an  OGNL expression or a string literal by default.  It's 
entirely within the tag implementation.

So you're right, first the container evaluates the JSP EL, then the tag 
evaluates the value as OGNL.  However, in Struts 2.0.11 all of the 
Struts tags have disallowed the use of JSP EL in their attributes, so 
that never actually happens any more.  The rationale is a vulerability 
when user submitted data is processed as valid JSP EL into valid OGNL.

${x} is the JSP EL notation supported by the container
%{x} is the OGNL notation supported by Struts2 tags.  The %{} is simply 
optional sometimes because some attributes are assumed to be OGNL 
expressions and some not.  I almost always include the %{} just for 
clarity.  eg. <s:if test="1 < 5"> is the same as <s:if test="%{1<5}">

The #x notation you refered to is part of OGNL itself and it means "the 
root object with name x".
The #{'x':'y'}notation is also part of OGNL and is used to create a root 
object of type map with an entry x:y.
The #{} notation also used by UEL to identify an expression for deferred 
evaluation (this is a conflict with the above notation so sometimes 
\#{'x':'y'} is used to prevent UEL from processing it)

I find the best way to think about OGNL is that Struts2 has setup a Map 
of objects for you.  By default OGNL is addressing the instance of your 
action in that map, but the map also contains the request, session and 
some other useful 'root' objects.  When you use <s:property value="x"> 
by default the tag is evaluating "x" against the default object (your 
action) which calls getX().  If use use a ModelDriven action the OGNL is 
evaluated against the model object.  If you use #x you're evaluating the 
root object named x.

In summary: the JSP EL is evaluated first, but it can be disallowed for 
certain tags and is disallowed for all struts 2.0.11+ tags.
The OGNL is evaluated internally by Struts2 tags.  It's addressing an 
object graph setup by struts2.

The interaction between the JSP EL and OGNL in Struts2.0.11+ is that JSP 
EL can be used to place objects into a scope (eg. page, request, 
session) and OGNL can be used to get a value from that scope, and vice 
versa.  eg. s:set will put an object into a particular scope and it's be 
accessible to JSP EL.  The #attr, #request, #session objects can be used 
in an OGNL expression to get values out of the respective scopes.

The advantage of OGNL over JSP EL is that it's very sophisticated and 
powerful.  It's disadvantage compared to JSP EL is also that it's very 
sophisticated and powerful.

I hope that's useful information and reasonably accurate.
Jeromy Evans

References:
[JSP2.1] http://jcp.org/aboutJava/communityprocess/final/jsr245/index.html
[JSP2.0] http://java.sun.com/products/jsp/docs.html
[OGNL] http://struts.apache.org/2.x/docs/ognl.html


José Cervera wrote:
> Hi,
>
> I'm using Struts 2.0.11.
>
> I'm a bit confused with the available "expression languages". I've found
> that ${ ... }, %{ ... } and #... are all possible, but I'd like to know who
> defines them, in which tags they are available, in which scopes they are
> available, etc.
>
> If I understand correctly, the ${ ... } is replaced by the servlet engine (
> e.g. Tomcat), so if I include it in a struts Tag, it will be replaced by a
> literal before it arrives to struts. Besides, this makes it available
> outside a tag. For instance,
>
> <html><body>
> ${myVar}
> </body></html>
>
> will display the contents of myVar if the server complies with JSP2.0.
>
> Expressions with #... are OGNL, and are replaced by struts (webworks),
> right?
>
> What about the %{ ... } ?
>
> Is there a document describing how all these options fit together?
>
> Thanks in advance.
>
>   
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.516 / Virus Database: 269.17.13/1211 - Release Date: 6/01/2008 11:57 AM
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org