You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Kaspar Fischer <fi...@inf.ethz.ch> on 2007/11/26 19:49:40 UTC

Dynamic pages (or: arguments to pages)

Hi All,

I am new to Tapestry and would be glad to receive a hint on how to  
realize
"dynamic pages", i.e., pages whose content depends on GET arguments.

I have a database full of articles, each having an Id, and I want to
show each article on its own page. In PHP, I would have used URLs like

   http://my.organization.org/app?page=article&id=2832

to display article with Id 2832.

I am not quite sure what the Tapestry way for such a problem is. I've
coded a page ArticlePage that extends BasePage and now need a way to
learn the Id from the URL. How can I do this?

Many thanks,
Kaspar

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


Re: Dynamic pages (or: arguments to pages) [T4]

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
On 29.11.2007, at 11:02, Ulrich Stärk wrote:

> These are just guesses, I haven't tried them nor verified them.
> I could imagine that the parameter isn't available via the  
> WebRequest because the page didn't get called by a GET request  
> directly but with the help of the service encoder, so your  
> parameter isn't there anymore when the page get's rendered. Try to  
> get hold of the IRequestCycle by calling getRequestCycle() and call  
> getParameter on it. I wouldn't be surprised if it's in there.

Hi Uli,

That was it! It turns out that the

   @InjectObject("infrastructure:request")
   public abstract WebRequest getRequest();

is not needed anymore with this solution.

Thanks a lot for the help,
Kaspar

P.S. For the sake of completeness, here is the complete
solution -- in case somebody else needs it.

public abstract class MyPage extends BasePage
   implements PageBeginRenderListener {

   public void pageBeginRender(PageEvent event)
   {
     super.pageBeginRender(event);

     // find out the node we're suppost to show
     IRequestCycle cycle = getRequestCycle();
     String myParam = cycle.getParameter("myParam");
   }

   // ...
}

public class KCNodeServiceEncoder implements ServiceEncoder {
   // ...

   public void decode(ServiceEncoding encoding)
   {
     // ... (get parameter value from URL)

     encoding.setParameterValue("myParam", value);
   }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Dynamic pages (or: arguments to pages) [T4]

Posted by Ulrich Stärk <ul...@spielviel.de>.
These are just guesses, I haven't tried them nor verified them.
I could imagine that the parameter isn't available via the WebRequest 
because the page didn't get called by a GET request directly but with 
the help of the service encoder, so your parameter isn't there anymore 
when the page get's rendered. Try to get hold of the IRequestCycle by 
calling getRequestCycle() and call getParameter on it. I wouldn't be 
surprised if it's in there.

Cheers,

Uli

Kaspar Fischer schrieb:
> Unfortunately, my solution does not really work yet. My page
> uses
> 
>   @InjectObject("infrastructure:request")
>   public abstract WebRequest getRequest();
> 
>   public void pageBeginRender(PageEvent event)
>   {
>     super.pageBeginRender(event);
> 
>     WebRequest request = getRequest();
>     id = request.getParameterValue("id")); // (*)
>   }
> 
> to read the Id of the page to display. This indeed works:
> 
>   http://localhost/app?id=someid
> 
> results in id from (*) being set to "someid".
> 
> I now wanted to support friendly URLs and wrote a ServiceEncoder
> whose decode() method extracts the Id form a URL like
> 
>   http://localhost/node/id.html
> 
> and sets it:
> 
>   public void decode(ServiceEncoding encoding)
>   {
>     // ...
>     String id = // ...
> 
>     encoding.setParameterValue(ServiceConstants.SERVICE, 
> Tapestry.PAGE_SERVICE);
>     encoding.setParameterValue(ServiceConstants.PAGE, _className);
>     encoding.setParameterValue("id", id);
> }
> 
> But although the variable encoding contains the parameter "id"
> with its value (I verifed this), the page's pageBeginRender() from
> above receives null.
> 
> I feel I misunderstand something here. Why are parameters not
> passed on to the page request?
> 
> Many thanks,
> Kaspar
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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


Re: Dynamic pages (or: arguments to pages) [T4]

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
Unfortunately, my solution does not really work yet. My page
uses

   @InjectObject("infrastructure:request")
   public abstract WebRequest getRequest();

   public void pageBeginRender(PageEvent event)
   {
     super.pageBeginRender(event);

     WebRequest request = getRequest();
     id = request.getParameterValue("id")); // (*)
   }

to read the Id of the page to display. This indeed works:

   http://localhost/app?id=someid

results in id from (*) being set to "someid".

I now wanted to support friendly URLs and wrote a ServiceEncoder
whose decode() method extracts the Id form a URL like

   http://localhost/node/id.html

and sets it:

   public void decode(ServiceEncoding encoding)
   {
     // ...
     String id = // ...

     encoding.setParameterValue(ServiceConstants.SERVICE,  
Tapestry.PAGE_SERVICE);
     encoding.setParameterValue(ServiceConstants.PAGE, _className);
     encoding.setParameterValue("id", id);
}

But although the variable encoding contains the parameter "id"
with its value (I verifed this), the page's pageBeginRender() from
above receives null.

I feel I misunderstand something here. Why are parameters not
passed on to the page request?

Many thanks,
Kaspar

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


Re: Dynamic pages (or: arguments to pages) [T4]

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
Ulrich, Andy, Jesse, Jonathan, and Andy, thanks a lot for
the many responses and tips!

I am using T4 and have implemented it by injecting the web
request into my page and using a URL encoder for friendly URLs.

Regards,
Kaspar

On 27.11.2007, at 01:54, Andy Huhn wrote:

> Kaspar,
>
> If you're using T5, see
>
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/pagenav.html
>
> Especially the section titled "Page activation".
>
> Andy
>
> On Mon, 2007-11-26 at 19:49 +0100, Kaspar Fischer wrote:
>> Hi All,
>>
>> I am new to Tapestry and would be glad to receive a hint on how to
>> realize
>> "dynamic pages", i.e., pages whose content depends on GET arguments.
>>
>> I have a database full of articles, each having an Id, and I want to
>> show each article on its own page. In PHP, I would have used URLs  
>> like
>>
>>    http://my.organization.org/app?page=article&id=2832
>>
>> to display article with Id 2832.
>>
>> I am not quite sure what the Tapestry way for such a problem is. I've
>> coded a page ArticlePage that extends BasePage and now need a way to
>> learn the Id from the URL. How can I do this?
>>
>> Many thanks,
>> Kaspar
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>


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


Re: Dynamic pages (or: arguments to pages)

Posted by Andy Huhn <am...@insightbb.com>.
Kaspar,

If you're using T5, see

http://tapestry.apache.org/tapestry5/tapestry-core/guide/pagenav.html

Especially the section titled "Page activation".

Andy

On Mon, 2007-11-26 at 19:49 +0100, Kaspar Fischer wrote:
> Hi All,
> 
> I am new to Tapestry and would be glad to receive a hint on how to  
> realize
> "dynamic pages", i.e., pages whose content depends on GET arguments.
> 
> I have a database full of articles, each having an Id, and I want to
> show each article on its own page. In PHP, I would have used URLs like
> 
>    http://my.organization.org/app?page=article&id=2832
> 
> to display article with Id 2832.
> 
> I am not quite sure what the Tapestry way for such a problem is. I've
> coded a page ArticlePage that extends BasePage and now need a way to
> learn the Id from the URL. How can I do this?
> 
> Many thanks,
> Kaspar
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org

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


RE: Dynamic pages (or: arguments to pages)

Posted by Jonathan Barker <jo...@gmail.com>.
Assuming T4...

Assuming that you just want a readable URL but not necessarily a friendly
one, then you can use ExternalLinks to pages implementing IExternalPage.

Make your page implement IExternalPage.  In your 	

public void activateExternalPage(Object[] parameters, IRequestCycle cycle) 

method, extract the parameter by position: parameters[0].

Then you would have something like
http://my.organization.org/app?service=external&page=article&sp=l2832


I think if you look at the URL encoders as suggested by Jesse, there is a
nice transition from the External link above to a "prettier" URL.


Jonathan

> -----Original Message-----
> From: Kaspar Fischer [mailto:fischerk@inf.ethz.ch]
> Sent: Monday, November 26, 2007 1:50 PM
> To: Tapestry users
> Subject: Dynamic pages (or: arguments to pages)
> 
> Hi All,
> 
> I am new to Tapestry and would be glad to receive a hint on how to
> realize
> "dynamic pages", i.e., pages whose content depends on GET arguments.
> 
> I have a database full of articles, each having an Id, and I want to
> show each article on its own page. In PHP, I would have used URLs like
> 
>    http://my.organization.org/app?page=article&id=2832
> 
> to display article with Id 2832.
> 
> I am not quite sure what the Tapestry way for such a problem is. I've
> coded a page ArticlePage that extends BasePage and now need a way to
> learn the Id from the URL. How can I do this?
> 
> Many thanks,
> Kaspar
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org


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


Re: Dynamic pages (or: arguments to pages)

Posted by Jesse Kuhnert <jk...@gmail.com>.
Don't forget to study up on using the url encoders,  which would let
you change that url to be something more like:

 http://my.organization.org/app/page/article/2832.html

(or maybe just app/page/article/2832)

http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/engine/encoders/

On Nov 26, 2007 2:40 PM, andyhot <an...@di.uoa.gr> wrote:
> Also, if you have access to IRequestCycle, you can do getParameter or
> getParameters on it
>
> But first, read about the external service and about implementing
> IExternalPage
>
>
>
> Ulrich Stärk wrote:
> > With Tapestry 4.1 you would inject the WebRequest object and call
> > getParameterValue() on it (see
> > http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/web/WebRequest.html).
> >
> > To inject the WebRequest use
> >
> > @InjectObject("infrastructure:request")
> > public abstract WebRequest getRequest();
> >
> > in your page class.
> >
> > Cheers,
> >
> > Uli
> >
> > Kaspar Fischer schrieb:
> >> Hi All,
> >>
> >> I am new to Tapestry and would be glad to receive a hint on how to
> >> realize
> >> "dynamic pages", i.e., pages whose content depends on GET arguments.
> >>
> >> I have a database full of articles, each having an Id, and I want to
> >> show each article on its own page. In PHP, I would have used URLs like
> >>
> >>   http://my.organization.org/app?page=article&id=2832
> >>
> >> to display article with Id 2832.
> >>
> >> I am not quite sure what the Tapestry way for such a problem is. I've
> >> coded a page ArticlePage that extends BasePage and now need a way to
> >> learn the Id from the URL. How can I do this?
> >>
> >> Many thanks,
> >> Kaspar
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
> --
> Andreas Andreou - andyhot@apache.org - http://blog.andyhot.gr
> Tapestry / Tacos developer
> Open Source / JEE Consulting
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Jesse Kuhnert
Tapestry/Dojo team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind. http://blog.opencomponentry.com

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


Re: Dynamic pages (or: arguments to pages)

Posted by Ulrich Stärk <ul...@spielviel.de>.
Why exactly do you think one should use the external service here? And 
what is the benefit of it anyway? To my knowledge the only "difference" 
is the ability to bookmark pages. But that can be done with "normal" 
pages too.
As to parameter extraction I don't like the external service solution at 
all. Parameters don't get extracted by name but by order. If someone 
changes the parameters' order the result will be different then expected.

Cheers,

Uli

andyhot schrieb:
> Also, if you have access to IRequestCycle, you can do getParameter or 
> getParameters on it
> 
> But first, read about the external service and about implementing 
> IExternalPage
> 
> 
> Ulrich Stärk wrote:
>> With Tapestry 4.1 you would inject the WebRequest object and call 
>> getParameterValue() on it (see 
>> http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/web/WebRequest.html). 
>>
>> To inject the WebRequest use
>>
>> @InjectObject("infrastructure:request")
>> public abstract WebRequest getRequest();
>>
>> in your page class.
>>
>> Cheers,
>>
>> Uli
>>
>> Kaspar Fischer schrieb:
>>> Hi All,
>>>
>>> I am new to Tapestry and would be glad to receive a hint on how to 
>>> realize
>>> "dynamic pages", i.e., pages whose content depends on GET arguments.
>>>
>>> I have a database full of articles, each having an Id, and I want to
>>> show each article on its own page. In PHP, I would have used URLs like
>>>
>>>   http://my.organization.org/app?page=article&id=2832
>>>
>>> to display article with Id 2832.
>>>
>>> I am not quite sure what the Tapestry way for such a problem is. I've
>>> coded a page ArticlePage that extends BasePage and now need a way to
>>> learn the Id from the URL. How can I do this?
>>>
>>> Many thanks,
>>> Kaspar
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
> 


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


Re: Dynamic pages (or: arguments to pages)

Posted by andyhot <an...@di.uoa.gr>.
Also, if you have access to IRequestCycle, you can do getParameter or 
getParameters on it

But first, read about the external service and about implementing 
IExternalPage


Ulrich Stärk wrote:
> With Tapestry 4.1 you would inject the WebRequest object and call 
> getParameterValue() on it (see 
> http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/web/WebRequest.html). 
>
> To inject the WebRequest use
>
> @InjectObject("infrastructure:request")
> public abstract WebRequest getRequest();
>
> in your page class.
>
> Cheers,
>
> Uli
>
> Kaspar Fischer schrieb:
>> Hi All,
>>
>> I am new to Tapestry and would be glad to receive a hint on how to 
>> realize
>> "dynamic pages", i.e., pages whose content depends on GET arguments.
>>
>> I have a database full of articles, each having an Id, and I want to
>> show each article on its own page. In PHP, I would have used URLs like
>>
>>   http://my.organization.org/app?page=article&id=2832
>>
>> to display article with Id 2832.
>>
>> I am not quite sure what the Tapestry way for such a problem is. I've
>> coded a page ArticlePage that extends BasePage and now need a way to
>> learn the Id from the URL. How can I do this?
>>
>> Many thanks,
>> Kaspar
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

-- 
Andreas Andreou - andyhot@apache.org - http://blog.andyhot.gr
Tapestry / Tacos developer
Open Source / JEE Consulting


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


Re: Dynamic pages (or: arguments to pages)

Posted by Ulrich Stärk <ul...@spielviel.de>.
With Tapestry 4.1 you would inject the WebRequest object and call 
getParameterValue() on it (see 
http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/web/WebRequest.html).
To inject the WebRequest use

@InjectObject("infrastructure:request")
public abstract WebRequest getRequest();

in your page class.

Cheers,

Uli

Kaspar Fischer schrieb:
> Hi All,
> 
> I am new to Tapestry and would be glad to receive a hint on how to realize
> "dynamic pages", i.e., pages whose content depends on GET arguments.
> 
> I have a database full of articles, each having an Id, and I want to
> show each article on its own page. In PHP, I would have used URLs like
> 
>   http://my.organization.org/app?page=article&id=2832
> 
> to display article with Id 2832.
> 
> I am not quite sure what the Tapestry way for such a problem is. I've
> coded a page ArticlePage that extends BasePage and now need a way to
> learn the Id from the URL. How can I do this?
> 
> Many thanks,
> Kaspar
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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