You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Gamble, Wesley (WG10)" <WG...@tmw.com> on 2008/06/10 23:33:45 UTC

Dynamically pulling in tile definitions based on session attributes using OGNL

All,

 

Struts 2.0.11.1

Tiles 2.0.6

 

Is the following valid in a given JSP fragment/Tile?  

 

I'm trying to use the "companyId" attribute of my session to help define
the name of a Tile definition.  

 

Assuming that session has a "companyId" attribute, of course.

 

   <tiles:insertDefinition name="${#session.companyId}_compensation" />

 

Thanks,

Wes

 

 


RE: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by "Gamble, Wesley (WG10)" <WG...@tmw.com>.
Antonio, Chris, Dave, Martin,

Thanks for the responses.  I haven't had a chance to thoroughly mull
them over, but I am confident that once I do so, my confusion will be
eased.

I'm quite keen on the combination of S2 and Tiles - and I'm sure I can
do what I need to.

Thanks again,
Wes

-----Original Message-----
From: Antonio Petrelli [mailto:antonio.petrelli@gmail.com] 
Sent: Wednesday, June 11, 2008 1:41 AM
To: Struts Users Mailing List
Subject: Re: Dynamically pulling in tile definitions based on session
attributes using OGNL

2008/6/10 Gamble, Wesley (WG10) <WG...@tmw.com>:
>   <tiles:insertDefinition name="${#session.companyId}_compensation" />

It does not work because Tiles (at the moment) does not support OGNL.
Notice that Tiles does not support JSP EL "directly". In fact, it is
the container that supports it.
In other words, *before* the tag is called, the EL expression is
evaluated by the container, and then transferred to the tag.

Antonio

---------------------------------------------------------------------
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: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by Antonio Petrelli <an...@gmail.com>.
2008/6/10 Gamble, Wesley (WG10) <WG...@tmw.com>:
>   <tiles:insertDefinition name="${#session.companyId}_compensation" />

It does not work because Tiles (at the moment) does not support OGNL.
Notice that Tiles does not support JSP EL "directly". In fact, it is
the container that supports it.
In other words, *before* the tag is called, the EL expression is
evaluated by the container, and then transferred to the tag.

Antonio

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


Re: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by Chris Pratt <th...@gmail.com>.
Then again you could just use:

<tiles:insertDefinition name="${companyId}_compensation" /> or
<tiles:insertDefinition name="${session.companyId}_compensation"/>

And ignore OGNL completely.  In the first line, since you seem to have
companyId in the session, and JSP EL searches through each scope to
find a value, it will find the value of companyId (unless it finds a
value in the page or request scopes first).  The second makes sure it
looks in the session attributes and forgoes the scope search.
  (*Chris*)


On Tue, Jun 10, 2008 at 3:59 PM, Dave Newton <ne...@yahoo.com> wrote:
> --- On Tue, 6/10/08, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
>> I'm not clear on the distinction between EL and OGNL.
>
> OGNL is an EL, it's just not JSP EL.
>
>> As a rule of thumb, would it be correct to say that OGNL is
>> only valid within Struts tags, and I should never expect OGNL
>> interpolation in any other context?
>
> Yes, with the caveat that OGNL is also valid inside S2 configuration files when surrounded by ${}, the JSP EL escapes.
>
> (Don't ask, I don't know.)
>
>> I thought that OGNL allowed me to get session attributes
>> via the $(#session.attributeId} syntax?
>
> Why? Is that somewhere in the S2 docs? If so, I'll fix it, because it's wrong.
>
> ${} (in the context of a JSP page) is JSP EL, evaluated by the container. "#" is an OGNL character used to access a named value and is valid only inside OGNL expressions... but.
>
> But if you're running a JSP 2.1+ (2.2? I can never remember) container and attempt to use OGNL things will blow up, because # is a JSF/JSP 2.1+ escape, so the container tries to evaluate the following expression.
>
>> I'm able to do what I want with this:
>> <s:set name="companyId" value="#session.companyId" />
>> <tiles:insertDefinition name="${companyId}_compensation" />
>>
>> Why does this work?
>
> Because the first tag is using pure OGNL properly, and the second one is using JSP EL properly, but...
>
>> Am I using OGNL in the first line (#session.companyId) to set
>> a JSP variable which I can then access in the 2nd line?
>
> The <s:set.../> creates a variable in the stack context.
>
> The reason the <tiles.../> works is because (a) if you already/still have a normal JEE *scoped* variable (i.e., in session, request, etc.) it will find it via the normal JSP EL lookup, and/or (b) S2 uses a custom request mapper that will go to the stack context when a value isn't found in any of the normal JEE scopes.
>
>> The 2nd line looks like OGNL as well to me, but maybe it's EL.
>
> It shouldn't look like OGNL.
>
> I'd strongly recommend spending some time with the S2 docs (and potentially some JEE docs if necessary); they should be able to clear up all these issues.
>
> Dave
>
>
> ---------------------------------------------------------------------
> 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: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by Dave Newton <ne...@yahoo.com>.
--- On Tue, 6/10/08, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
> I'm not clear on the distinction between EL and OGNL.

OGNL is an EL, it's just not JSP EL.

> As a rule of thumb, would it be correct to say that OGNL is
> only valid within Struts tags, and I should never expect OGNL
> interpolation in any other context?

Yes, with the caveat that OGNL is also valid inside S2 configuration files when surrounded by ${}, the JSP EL escapes.

(Don't ask, I don't know.)

> I thought that OGNL allowed me to get session attributes
> via the $(#session.attributeId} syntax?

Why? Is that somewhere in the S2 docs? If so, I'll fix it, because it's wrong.

${} (in the context of a JSP page) is JSP EL, evaluated by the container. "#" is an OGNL character used to access a named value and is valid only inside OGNL expressions... but.

But if you're running a JSP 2.1+ (2.2? I can never remember) container and attempt to use OGNL things will blow up, because # is a JSF/JSP 2.1+ escape, so the container tries to evaluate the following expression.

> I'm able to do what I want with this:
> <s:set name="companyId" value="#session.companyId" />
> <tiles:insertDefinition name="${companyId}_compensation" />
> 
> Why does this work?

Because the first tag is using pure OGNL properly, and the second one is using JSP EL properly, but...

> Am I using OGNL in the first line (#session.companyId) to set 
> a JSP variable which I can then access in the 2nd line?

The <s:set.../> creates a variable in the stack context.

The reason the <tiles.../> works is because (a) if you already/still have a normal JEE *scoped* variable (i.e., in session, request, etc.) it will find it via the normal JSP EL lookup, and/or (b) S2 uses a custom request mapper that will go to the stack context when a value isn't found in any of the normal JEE scopes.

> The 2nd line looks like OGNL as well to me, but maybe it's EL.

It shouldn't look like OGNL.

I'd strongly recommend spending some time with the S2 docs (and potentially some JEE docs if necessary); they should be able to clear up all these issues.

Dave


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


RE: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by "Gamble, Wesley (WG10)" <WG...@tmw.com>.
Obviously, I'm not clear on the distinction between EL and OGNL.

As a rule of thumb, would it be correct to say that OGNL is only valid
within Struts tags, and I should never expect OGNL interpolation in any
other context?

I thought that OGNL allowed me to get session attributes via the
$(#session.attributeId} syntax?

I'm able to do what I want with this:

<s:set name="companyId" value="#session.companyId" />
<tiles:insertDefinition name="${companyId}_compensation" />

But this doesn't quite make sense to me.  Why does this work?  Am I
using OGNL in the first line (#session.companyId) to set a JSP variable
which I can then access in the 2nd line?  Is that "companyId" variable
placed on the value stack for access (it appears to be).  The 2nd line
looks like OGNL as well to me, but maybe it's EL.

In this case, the company's tiles are really that different.  And there
aren't many companies ;).

Wes

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com] 
Sent: Tuesday, June 10, 2008 4:41 PM
To: Struts Users Mailing List
Subject: Re: Dynamically pulling in tile definitions based on session
attributes using OGNL

--- On Tue, 6/10/08, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
> <tiles:insertDefinition name="${#session.companyId}_compensation" />

You're still mixing JSP EL and OGNL. The link I supplied earlier might
still be handy (and I provided a direct link to your other question
regarding interceptor access to various servlet-oriented structures).

That aside, are company's tiles really so different that you need a
different tile for each company? (Rhetorical.) If so, I hope there
aren't very many companies :/

Dave


---------------------------------------------------------------------
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: Dynamically pulling in tile definitions based on session attributes using OGNL

Posted by Dave Newton <ne...@yahoo.com>.
--- On Tue, 6/10/08, Gamble, Wesley (WG10) <WG...@tmw.com> wrote:
> <tiles:insertDefinition name="${#session.companyId}_compensation" />

You're still mixing JSP EL and OGNL. The link I supplied earlier might still be handy (and I provided a direct link to your other question regarding interceptor access to various servlet-oriented structures).

That aside, are company's tiles really so different that you need a different tile for each company? (Rhetorical.) If so, I hope there aren't very many companies :/

Dave


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