You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Matt Raible <li...@raibledesigns.com> on 2004/12/01 07:26:34 UTC

Can I access servlet context objects with OGNL?

I'm displaying a simple table of active users that login to my webapp.
I use an HttpSessionListener to stuff new users into a Set when they
login, then this set is stored in the ServletContext.  Is there a simple
way with OGNL to get an object from the servlet context?

Currently, it seems like I have to jump through hoops, which I know is
not the "Tapestry way".  The following works, but when I click on the
table heading I get an error:

Either the tableModel parameter or both source and columns parameters
must be specified by component activeUsers/table.tableView
Stack Trace:

    *
org.apache.tapestry.contrib.table.components.TableView.getTableModel(TableView.java:199)
    *
org.apache.tapestry.contrib.table.components.inserted.SimpleTableColumnComponent.columnSelected(SimpleTableColumnComponent.java:150)
    * sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

The error seems strange, since I *am* specifying both the source and
columns parameters.

activeUsers.html
---------------------------------------
<table jwcid="table@contrib:Table" class="list activeUserList" id="user"
    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
    columns="user.username:username, activeUsers.fullName:fullName"
    source="ognl:activeUsers" initialSortColumn="username"
    arrowUpAsset="ognl:assets.upArrow"
arrowDownAsset="ognl:assets.downArrow">
</table>

activeUsers.page
---------------------------------------
<page-specification class="org.appfuse.webapp.action.ActiveUserList">
    <property-specification name="activeUsers" type="java.util.Set"/>
    <property-specification name="row"/>

    <bean name="evenOdd" class="org.apache.tapestry.bean.EvenOdd"/>
    <context-asset name="upArrow" path="/images/arrow_up.png"/>
    <context-asset name="downArrow" path="/images/arrow_down.png"/>
</page-specification>

ActiveUserList.java
---------------------------------------
public abstract class ActiveUserList extends BasePage implements
PageRenderListener {
    public abstract Set getActiveUsers();

    public abstract void setActiveUsers(Set users);

    public void pageBeginRender(PageEvent event) {
        ServletContext context = getServletContext(getRequestCycle());
        Set users = (Set)
context.getAttribute(UserCounterListener.USERS_KEY);
        log.debug("setting activeUsers: " + users);
        setActiveUsers(users);
    }
}

Thanks,

Matt




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


Re: Can I access servlet context objects with OGNL?

Posted by Jamie Orchard-Hays <ja...@dang.com>.
doesn't matter where it's persistent.


----- Original Message ----- 
From: "Erik Hatcher" <er...@ehatchersolutions.com>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Wednesday, December 01, 2004 1:36 PM
Subject: Re: Can I access servlet context objects with OGNL?


> On Dec 1, 2004, at 1:30 PM, Jamie Orchard-Hays wrote:
>> You probably need to make your source persistent on the page.
> 
> Why?  If the data is *persistent* somewhere then what difference does  
> it make where?
> 
> I've used visit objects for a source with no problems.
> 
> Erik
> 
> 
>>
>>
>>
>> ----- Original Message ----- From: "Matt Raible"  
>> <li...@raibledesigns.com>
>> To: "Tapestry users" <ta...@jakarta.apache.org>
>> Sent: Wednesday, December 01, 2004 1:12 PM
>> Subject: Re: Can I access servlet context objects with OGNL?
>>
>>
>>> Erik Hatcher wrote:
>>>> On Dec 1, 2004, at 1:26 AM, Matt Raible wrote:
>>>>
>>>>> Is there a simple
>>>>> way with OGNL to get an object from the servlet context?
>>>>
>>>>
>>>> Simple?  That's a matter of opinion, I suppose, but yes.  Keep in  
>>>> mind  that OGNL expressions are relative to the page/component they  
>>>> are  in...
>>>>
>>>>> Either the tableModel parameter or both source and columns  
>>>>> parameters
>>>>> must be specified by component activeUsers/table.tableView
>>>>
>>>>
>>>> This is because your pageBeginRender has not executed yet.  But the   
>>>> following change should fix it up. '
>>> It works the first time the table is displayed, but bombs when I  
>>> click  on a column heading.
>>>>
>>>>> activeUsers.html
>>>>> ---------------------------------------
>>>>> <table jwcid="table@contrib:Table" class="list activeUserList"   
>>>>> id="user"
>>>>>    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
>>>>>    columns="user.username:username, activeUsers.fullName:fullName"
>>>>>    source="ognl:activeUsers" initialSortColumn="username"
>>>>>    arrowUpAsset="ognl:assets.upArrow"
>>>>> arrowDownAsset="ognl:assets.downArrow">
>>>>> </table>
>>>>>
>>>>> ActiveUserList.java
>>>>> ---------------------------------------
>>>>> public abstract class ActiveUserList extends BasePage implements
>>>>> PageRenderListener {
>>>>>    public abstract Set getActiveUsers();
>>>>>
>>>>>    public abstract void setActiveUsers(Set users);
>>>>>
>>>>>    public void pageBeginRender(PageEvent event) {
>>>>>        ServletContext context =  
>>>>> getServletContext(getRequestCycle());
>>>>>        Set users = (Set)
>>>>> context.getAttribute(UserCounterListener.USERS_KEY);
>>>>>        log.debug("setting activeUsers: " + users);
>>>>>        setActiveUsers(users);
>>>>>    }
>>>>> }
>>>>
>>>>
>>>> Notice you are calling getRequestCycle().getAttribute(key).  To do   
>>>> this with OGNL, change your source to be:
>>>>
>>>>     ognl:requestCycle.getAttribute(@UserCounterListener@USERS_KEY)
>>> I tried this, as well as the following, with no luck:
>>> ognl:  
>>> requestCycle.getRequestContext().getServlet().getServletContext().getA 
>>> tt ribute(@org.appfuse.webapp.listener.UserCounterListener@USERS_KEY)
>>> ognl:  
>>> requestCycle.getRequestContext().getServlet().getServletContext().getA 
>>> tt ribute('userNames')
>>> Kinda makes you long for JSTL and the display tag.
>>> <display:table name="applicationScope.userNames" .../>
>>> ;-)
>>> Matt
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>

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


Re: Can I access servlet context objects with OGNL?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Dec 1, 2004, at 1:30 PM, Jamie Orchard-Hays wrote:
> You probably need to make your source persistent on the page.

Why?  If the data is *persistent* somewhere then what difference does  
it make where?

I've used visit objects for a source with no problems.

	Erik


>
>
>
> ----- Original Message ----- From: "Matt Raible"  
> <li...@raibledesigns.com>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, December 01, 2004 1:12 PM
> Subject: Re: Can I access servlet context objects with OGNL?
>
>
>> Erik Hatcher wrote:
>>> On Dec 1, 2004, at 1:26 AM, Matt Raible wrote:
>>>
>>>> Is there a simple
>>>> way with OGNL to get an object from the servlet context?
>>>
>>>
>>> Simple?  That's a matter of opinion, I suppose, but yes.  Keep in  
>>> mind  that OGNL expressions are relative to the page/component they  
>>> are  in...
>>>
>>>> Either the tableModel parameter or both source and columns  
>>>> parameters
>>>> must be specified by component activeUsers/table.tableView
>>>
>>>
>>> This is because your pageBeginRender has not executed yet.  But the   
>>> following change should fix it up. '
>> It works the first time the table is displayed, but bombs when I  
>> click  on a column heading.
>>>
>>>> activeUsers.html
>>>> ---------------------------------------
>>>> <table jwcid="table@contrib:Table" class="list activeUserList"   
>>>> id="user"
>>>>    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
>>>>    columns="user.username:username, activeUsers.fullName:fullName"
>>>>    source="ognl:activeUsers" initialSortColumn="username"
>>>>    arrowUpAsset="ognl:assets.upArrow"
>>>> arrowDownAsset="ognl:assets.downArrow">
>>>> </table>
>>>>
>>>> ActiveUserList.java
>>>> ---------------------------------------
>>>> public abstract class ActiveUserList extends BasePage implements
>>>> PageRenderListener {
>>>>    public abstract Set getActiveUsers();
>>>>
>>>>    public abstract void setActiveUsers(Set users);
>>>>
>>>>    public void pageBeginRender(PageEvent event) {
>>>>        ServletContext context =  
>>>> getServletContext(getRequestCycle());
>>>>        Set users = (Set)
>>>> context.getAttribute(UserCounterListener.USERS_KEY);
>>>>        log.debug("setting activeUsers: " + users);
>>>>        setActiveUsers(users);
>>>>    }
>>>> }
>>>
>>>
>>> Notice you are calling getRequestCycle().getAttribute(key).  To do   
>>> this with OGNL, change your source to be:
>>>
>>>     ognl:requestCycle.getAttribute(@UserCounterListener@USERS_KEY)
>> I tried this, as well as the following, with no luck:
>> ognl:  
>> requestCycle.getRequestContext().getServlet().getServletContext().getA 
>> tt ribute(@org.appfuse.webapp.listener.UserCounterListener@USERS_KEY)
>> ognl:  
>> requestCycle.getRequestContext().getServlet().getServletContext().getA 
>> tt ribute('userNames')
>> Kinda makes you long for JSTL and the display tag.
>> <display:table name="applicationScope.userNames" .../>
>> ;-)
>> Matt
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


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


Re: Can I access servlet context objects with OGNL?

Posted by Jamie Orchard-Hays <ja...@dang.com>.
You probably need to make your source persistent on the page.



----- Original Message ----- 
From: "Matt Raible" <li...@raibledesigns.com>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Wednesday, December 01, 2004 1:12 PM
Subject: Re: Can I access servlet context objects with OGNL?


> Erik Hatcher wrote:
> 
>> On Dec 1, 2004, at 1:26 AM, Matt Raible wrote:
>>
>>> Is there a simple
>>> way with OGNL to get an object from the servlet context?
>>
>>
>> Simple?  That's a matter of opinion, I suppose, but yes.  Keep in mind  
>> that OGNL expressions are relative to the page/component they are  
>> in...
>>
>>> Either the tableModel parameter or both source and columns parameters
>>> must be specified by component activeUsers/table.tableView
>>
>>
>> This is because your pageBeginRender has not executed yet.  But the  
>> following change should fix it up. '
> 
> It works the first time the table is displayed, but bombs when I click  
> on a column heading.
> 
>>
>>> activeUsers.html
>>> ---------------------------------------
>>> <table jwcid="table@contrib:Table" class="list activeUserList"  
>>> id="user"
>>>    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
>>>    columns="user.username:username, activeUsers.fullName:fullName"
>>>    source="ognl:activeUsers" initialSortColumn="username"
>>>    arrowUpAsset="ognl:assets.upArrow"
>>> arrowDownAsset="ognl:assets.downArrow">
>>> </table>
>>>
>>> ActiveUserList.java
>>> ---------------------------------------
>>> public abstract class ActiveUserList extends BasePage implements
>>> PageRenderListener {
>>>    public abstract Set getActiveUsers();
>>>
>>>    public abstract void setActiveUsers(Set users);
>>>
>>>    public void pageBeginRender(PageEvent event) {
>>>        ServletContext context = getServletContext(getRequestCycle());
>>>        Set users = (Set)
>>> context.getAttribute(UserCounterListener.USERS_KEY);
>>>        log.debug("setting activeUsers: " + users);
>>>        setActiveUsers(users);
>>>    }
>>> }
>>
>>
>> Notice you are calling getRequestCycle().getAttribute(key).  To do  
>> this with OGNL, change your source to be:
>>
>>     ognl:requestCycle.getAttribute(@UserCounterListener@USERS_KEY)
> 
> 
> I tried this, as well as the following, with no luck:
> 
> ognl: 
> requestCycle.getRequestContext().getServlet().getServletContext().getAtt 
> ribute(@org.appfuse.webapp.listener.UserCounterListener@USERS_KEY)
> ognl: 
> requestCycle.getRequestContext().getServlet().getServletContext().getAtt 
> ribute('userNames')
> 
> Kinda makes you long for JSTL and the display tag.
> 
> <display:table name="applicationScope.userNames" .../>
> 
> ;-)
> 
> Matt
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>

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


Re: Can I access servlet context objects with OGNL?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Dec 1, 2004, at 1:12 PM, Matt Raible wrote:
> I tried this, as well as the following, with no luck:
>
> ognl: 
> requestCycle.getRequestContext().getServlet().getServletContext().getAt 
> tribute(@org.appfuse.webapp.listener.UserCounterListener@USERS_KEY)
> ognl: 
> requestCycle.getRequestContext().getServlet().getServletContext().getAt 
> tribute('userNames')

*shrug*

you don't need to use full method names if they don't take parameters:   
requestCycle.requestContext....

> Kinda makes you long for JSTL and the display tag.
>
> <display:table name="applicationScope.userNames" .../>
>
> ;-)

Well, if you were going completely with Tapestry instead of keeping  
your foot in every other framework it'd be trivial:

	source="ognl:global.userName"

:)

	Erik


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


Re: Can I access servlet context objects with OGNL?

Posted by Matt Raible <li...@raibledesigns.com>.
Erik Hatcher wrote:

> On Dec 1, 2004, at 1:26 AM, Matt Raible wrote:
>
>> Is there a simple
>> way with OGNL to get an object from the servlet context?
>
>
> Simple?  That's a matter of opinion, I suppose, but yes.  Keep in mind  
> that OGNL expressions are relative to the page/component they are  
> in...
>
>> Either the tableModel parameter or both source and columns parameters
>> must be specified by component activeUsers/table.tableView
>
>
> This is because your pageBeginRender has not executed yet.  But the  
> following change should fix it up. '

It works the first time the table is displayed, but bombs when I click  
on a column heading.

>
>> activeUsers.html
>> ---------------------------------------
>> <table jwcid="table@contrib:Table" class="list activeUserList"  
>> id="user"
>>    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
>>    columns="user.username:username, activeUsers.fullName:fullName"
>>    source="ognl:activeUsers" initialSortColumn="username"
>>    arrowUpAsset="ognl:assets.upArrow"
>> arrowDownAsset="ognl:assets.downArrow">
>> </table>
>>
>> ActiveUserList.java
>> ---------------------------------------
>> public abstract class ActiveUserList extends BasePage implements
>> PageRenderListener {
>>    public abstract Set getActiveUsers();
>>
>>    public abstract void setActiveUsers(Set users);
>>
>>    public void pageBeginRender(PageEvent event) {
>>        ServletContext context = getServletContext(getRequestCycle());
>>        Set users = (Set)
>> context.getAttribute(UserCounterListener.USERS_KEY);
>>        log.debug("setting activeUsers: " + users);
>>        setActiveUsers(users);
>>    }
>> }
>
>
> Notice you are calling getRequestCycle().getAttribute(key).  To do  
> this with OGNL, change your source to be:
>
>     ognl:requestCycle.getAttribute(@UserCounterListener@USERS_KEY)


I tried this, as well as the following, with no luck:

ognl: 
requestCycle.getRequestContext().getServlet().getServletContext().getAtt 
ribute(@org.appfuse.webapp.listener.UserCounterListener@USERS_KEY)
ognl: 
requestCycle.getRequestContext().getServlet().getServletContext().getAtt 
ribute('userNames')

Kinda makes you long for JSTL and the display tag.

<display:table name="applicationScope.userNames" .../>

;-)

Matt



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


Re: Can I access servlet context objects with OGNL?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Dec 1, 2004, at 1:26 AM, Matt Raible wrote:
> Is there a simple
> way with OGNL to get an object from the servlet context?

Simple?  That's a matter of opinion, I suppose, but yes.  Keep in mind 
that OGNL expressions are relative to the page/component they are in...

> Either the tableModel parameter or both source and columns parameters
> must be specified by component activeUsers/table.tableView

This is because your pageBeginRender has not executed yet.  But the 
following change should fix it up.

> activeUsers.html
> ---------------------------------------
> <table jwcid="table@contrib:Table" class="list activeUserList" 
> id="user"
>    rowsClass="ognl:beans.evenOdd.next" row="ognl:row"
>    columns="user.username:username, activeUsers.fullName:fullName"
>    source="ognl:activeUsers" initialSortColumn="username"
>    arrowUpAsset="ognl:assets.upArrow"
> arrowDownAsset="ognl:assets.downArrow">
> </table>
>
> ActiveUserList.java
> ---------------------------------------
> public abstract class ActiveUserList extends BasePage implements
> PageRenderListener {
>    public abstract Set getActiveUsers();
>
>    public abstract void setActiveUsers(Set users);
>
>    public void pageBeginRender(PageEvent event) {
>        ServletContext context = getServletContext(getRequestCycle());
>        Set users = (Set)
> context.getAttribute(UserCounterListener.USERS_KEY);
>        log.debug("setting activeUsers: " + users);
>        setActiveUsers(users);
>    }
> }

Notice you are calling getRequestCycle().getAttribute(key).  To do this 
with OGNL, change your source to be:

	ognl:requestCycle.getAttribute(@UserCounterListener@USERS_KEY)

You'll have to change the @UserCounterListener to have the fully 
qualified classname.

	Erik


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