You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gary Affonso <gl...@greywether.com> on 2007/10/19 18:25:18 UTC

Mixing Struts tags with naked EL expressions: ${}

I need some clarification on what is happening when I mix naked EL 
expressions with Struts tags in a JSP page.

To put this in context, lets say I have an action that makes a "product" 
object available to the view via a getter (in the action).  In the JSP 
view I can do this:

   ${product.name}

And I can do this...

   <s:iterator id="productItem" value="product.getProductItems()" >
     ${productItem.name}
   </s:iterator>

So this clearly works.  I can mix naked EL expressions and Struts tags 
into my JSP page.  The naked EL expressions have access to at least some 
portion of the OGNL stack.

So now I need some clarification as to what's really going on here.

Questions...


1) What EL language can I write the naked EL expression in?
Is it provided by the Servlet container?  I'm currently using Tomcat 5.5 
which implements JSP 2.0 and EL portion of JSP with Commons EL.  So I'm 
using commons EL, righ?

Following that logic, if I switched to GlassFish for my server, I'd then 
be using Unified EL, right?

Or is the EL available in these Struts JSP views something that struts 
is providing?  Like is Struts swapping in OGNL, MVEL, or JEXL behind the 
scenes?


2) What "context" is the EL evaluated against?
A JSP 2.0 "standard" expression has a bunch of implicit objects 
available to it (page, request, etc.) none of which would normally 
contain the "product" object that I reference in example above.

Nor woudl the productItem object normally be available to the EL 
expression, it's added to the top of the OGNL stack by the Struts 
iterator tag.

So Struts is obviously doing some magic to extend the standard EL context.

So how's that happening?  I'd like the details of the "context" the EL 
is executing against.  How is the OGNL stack shared with the EL context? 
  Is it just copying portions of the OGNL stack into the EL "page" 
context or something?


3) What libs are required to use EL in this way?
I've always associated EL with JSTL so I kinda figured that I'd need the 
JSTL libs.  But I just removed the JSTL libs from my project and the 
naked EL stuff still works great.  Also cool.

So now I'm thinking that all I need is a JSP 2.0 container (and the 
built-in libs it provides)?  Is that the requirement to make the EL 
magic work?


4) How do I get things like the "top" of the stack?
In the iterator example above I had to assign an ID to the iterator tag 
so that I had something to reference in my naked EL expression.

Is this a requirement or is there some naked EL expression that will let 
me get arbitrary properties of the OGNL stack (like the top, for example).

----

I sort of stumbled onto this combination of view-technologies after a 
bunch of googling.  And I'd go so far as to say this is *the* sweet-spot 
if I'm forced to use JSP views with Struts (instead of, say, Velocity or 
Freemarker).

It gives me the ability to do very clean string/value interpolations (I 
hate using tags for string/value interpolation) while still using the 
Struts taglib where appropriate.

Shouldn't this be doc'd better?  Or is it and I just missed it?  Am I 
the only one who thinks this rocks?

Thanks!

- Gary

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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Dave Newton <ne...@yahoo.com>.
Mmmm, soon it'll look like Perl. Or line noise.

Makes sense, though; that should get added to the wiki
somewhere if it hasn't already.

d.

--- Wes Wannemacher <we...@wantii.com> wrote:

> You can escape the # character so that UEL will not
> be invoked. In
> fact, this is necessary for anonymous OGNL list
> creation
> 
> i.e. -
> https://issues.apache.org/struts/browse/WW-2213
> 
> 
> -W
> 
> On 10/19/07, Dave Newton <ne...@yahoo.com>
> wrote:
> >
>
http://struts.apache.org/2.x/docs/can-we-use-jstl-with-the-framework.html
> >
> > --- Gary Affonso <gl...@greywether.com> wrote:
> > > Following that logic, if I switched to GlassFish
> for
> > > my server, I'd then be using Unified EL, right?
> >
> > Yes, but OGNL # expressions won't work unless you
> turn
> > it off, because # is a UEL escape char. See
> > http://struts.apache.org/2.x/docs/ognl.html.
> >
> > > 4) How do I get things like the "top" of the
> stack?
> >
> > [0]
> >
> > See
> >
> http://struts.apache.org/2.x/docs/ognl-basics.html.
> >
> > > Am I the only one who thinks this rocks?
> >
> > Probably.
> >
> > There are rumblings of changing either the use of
> > OGNL, making the EL pluggable, etc. but I'm not
> sure
> > where that's at in the process.
> >
> > d.
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> > For additional commands, e-mail:
> user-help@struts.apache.org
> >
> >
> 
> 
> -- 
> Wesley Wannemacher
> President, Head Engineer/Consultant
> WanTii, Inc.
> http://www.wantii.com
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Gary Affonso <gl...@greywether.com>.
Wes Wannemacher wrote:
> You can escape the # character so that UEL will not be invoked. In
> fact, this is necessary for anonymous OGNL list creation
> 
> i.e. - https://issues.apache.org/struts/browse/WW-2213

Good to know, thanks Wes.

- Gary

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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Wes Wannemacher <we...@wantii.com>.
You can escape the # character so that UEL will not be invoked. In
fact, this is necessary for anonymous OGNL list creation

i.e. - https://issues.apache.org/struts/browse/WW-2213


-W

On 10/19/07, Dave Newton <ne...@yahoo.com> wrote:
> http://struts.apache.org/2.x/docs/can-we-use-jstl-with-the-framework.html
>
> --- Gary Affonso <gl...@greywether.com> wrote:
> > Following that logic, if I switched to GlassFish for
> > my server, I'd then be using Unified EL, right?
>
> Yes, but OGNL # expressions won't work unless you turn
> it off, because # is a UEL escape char. See
> http://struts.apache.org/2.x/docs/ognl.html.
>
> > 4) How do I get things like the "top" of the stack?
>
> [0]
>
> See
> http://struts.apache.org/2.x/docs/ognl-basics.html.
>
> > Am I the only one who thinks this rocks?
>
> Probably.
>
> There are rumblings of changing either the use of
> OGNL, making the EL pluggable, etc. but I'm not sure
> where that's at in the process.
>
> d.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Wesley Wannemacher
President, Head Engineer/Consultant
WanTii, Inc.
http://www.wantii.com

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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Dave Newton <ne...@yahoo.com>.
--- Gary Affonso <gl...@greywether.com> wrote:
>
http://struts.apache.org/2.x/docs/exposing-framework-objects-to-jstl-with-a-jstl-and-displaytag-example.html
> 
> ...which, if I'm hearing you correctly, is
> unnecessary for the EL portion of JSTL.

That's probably true.

>
http://struts.apache.org/2.x/docs/using-struts-and-xwork-with-jsp-20-and-jstl-11.html
> 
> ...which is also, if I' hearing you correctly,
completely unnecessary.

That is probably also true.

> So maybe the docs should be updated?

Again, probably true.

At some point we'll need to garden the wiki to make
sure that everything there is relevent. That page, at
least, is pretty old.

>> Yes, but OGNL # expressions won't work unless you
>>  turn it off [...]
> Ahhh, understood.

I forgot about escaping it as Wes mentioned.

> >> 4) How do I get things like the "top" of the
> stack?
> > 
> > [0]
> >
> > See
> >
> http://struts.apache.org/2.x/docs/ognl-basics.html.
> 
> Hmmm.  How does that work, exactly?  Doesn't EL need
> an object identifier to reference the [] notation
against?

Don't just say "EL"; you have to say which one.
*Struts* tags use OGNL. The default object identifier
is "the stack". So if you say [0] that means
object-at-stack-location-0.

That's why when you're accessing an action property
named "baz" you can say <s:property value="baz"/>,
it's evaluating "baz" against the stack, and the first
place it looks is the stack top, which just after
hitting the view side is (more or less, anyway) your
action.

So there's a magic object reference already.

d.

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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Gary Affonso <gl...@greywether.com>.
Dave Newton wrote:
> http://struts.apache.org/2.x/docs/can-we-use-jstl-with-the-framework.html

Yah, I saw that.  But I also saw this...

http://struts.apache.org/2.x/docs/exposing-framework-objects-to-jstl-with-a-jstl-and-displaytag-example.html

...which, if I'm hearing you correctly, is unnecessary for the EL 
portion of JSTL. (And maybe for using JSTL tags at all).  And I saw this...

http://struts.apache.org/2.x/docs/using-struts-and-xwork-with-jsp-20-and-jstl-11.html

...which is also, if I' hearing you correctly, completely unnecessary.

So maybe the docs should be updated?

> Yes, but OGNL # expressions won't work unless you turn
> it off, because # is a UEL escape char. See
> http://struts.apache.org/2.x/docs/ognl.html.

Ahhh, understood.

So if I continue to mix naked EL expressions with Struts tags AND if I 
use a JSP 2.1 compatible container, then I'll have a problem with any 
OGNL "#" expressions I happen use in my struts tags.

Got it.  Good to know.

>> 4) How do I get things like the "top" of the stack?
> 
> [0]
>
> See
> http://struts.apache.org/2.x/docs/ognl-basics.html.

Hmmm.  How does that work, exactly?  Doesn't EL need an object 
identifier to reference the [] notation against?

   http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html

I know that [] brackets get overloaded to do property, map-key, or 
array-index lookups as appropriate.  But I was under the impression they 
needed to be evaluated against an object, not just naked like that.

>> Am I the only one who thinks this rocks?
> 
> Probably.
> 
> There are rumblings of changing either the use of
> OGNL, making the EL pluggable, etc. but I'm not sure
> where that's at in the process.

Yah, in my searches, I came across a lot of discussion (some of it 
mildly heated) about the future of Java Expression Languages.

So before sounding too enthusiastic, let me say that what I don't think 
rocks is that, in my JSP pages, I've got to keep in mind that there are 
two expression languages at work:

* naked ${} references are using the EL implementation provided by my 
Servlet container (in my case JSP 2.0 implemented by commons-EL)
* while expressions within Struts tags (the <property> tag for example) 
are using OGNL expressions.

That's just asking for trouble unless clearly spelled out in the code.

But I do LOVE the fact that I can do this:

   <a href="/images/${product.name}">${product.name}</a>

Instead of this:

   <a href="/images/<property value="product.getName()"/>">
     <property value="product.getName()"/>
   </a>

So with a mix of naked-EL expressions and Struts tags I can have my 
simple interpolation and still standardize against the Struts taglib. 
I'll be content with that for now.

What would *really* be great is if I could just do OGNL within the naked 
${} string-braces.  Now that would make me very happy.

Thanks for the clarifications, Dave!

- Gary

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


Re: Mixing Struts tags with naked EL expressions: ${}

Posted by Dave Newton <ne...@yahoo.com>.
http://struts.apache.org/2.x/docs/can-we-use-jstl-with-the-framework.html

--- Gary Affonso <gl...@greywether.com> wrote:
> Following that logic, if I switched to GlassFish for
> my server, I'd then be using Unified EL, right?

Yes, but OGNL # expressions won't work unless you turn
it off, because # is a UEL escape char. See
http://struts.apache.org/2.x/docs/ognl.html.

> 4) How do I get things like the "top" of the stack?

[0]

See
http://struts.apache.org/2.x/docs/ognl-basics.html.

> Am I the only one who thinks this rocks?

Probably.

There are rumblings of changing either the use of
OGNL, making the EL pluggable, etc. but I'm not sure
where that's at in the process.

d.


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