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 Kerr <ma...@centralparksoftware.com> on 2006/10/25 00:15:54 UTC
Reusable Component - which handles FORM/REQUEST values
I find Tapestry ok for generating html, but I'm struggling on the
request processing. I don't think I'm alone .. I see many references
to this topic on the mailing list archive -- and it's not very well
described in the documentation :-?
Specifically, reusable components participating in request processing.
How can my component gather information in the request, and return an
object to its caller (eg. the page, or another component). I'm
looking at the point in time *after* my component has received it's
request values, but *before* the page has invoked the listener action.
Also, I'd like to use an html template .. (What's the point in using
Tapestry if I'm writing my html *in the Java*??)
~~~
public void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
{
writer.begin("a");
writer.attribute("href", . . .);
writer.attribute("title", getTitle());
~~
I'm imagining something like .. FooPage and DatePicker component --
where the DatePicker hands a Date object to the page. A Date object
which the DatePicker creates from the FORM/request values sent to the
app. eg. ..
http://host:port/foo/blah?year=2006&month=10&day=24
What does DatePicker.java implementation look like ??
Can I use a template ? (Or do I have to write the html *in* the java
with IMarkupWriter, etc??)
Is there a callback for the point in time I mention above? .. where
the component is filled in by the framework, and it has an
opportunity to perform it's purpose, and then processing continues
elsewhere--like in the caller.
I believe "listener" callback isn't an option because you're not
guaranteed of the order.
The examples I'm reading online all break encapsulation and just have
the caller get the M/D/Y from the component so that the *caller*
creates the date. That's completely wrong -- and defeats the purpose
of a "reusable component" .. since the caller is doing the work.
Any ideas greatly appreciated.
My sketch goes something like this (below). I'm really wondering
what DatePicker.java does to push a value back to the caller *before*
the caller's action() method is called.
Thanks,
Matt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FooPage.html
~~~~~~~~~~~~
..
Start- <span jwcid="@DatePicker" date="ognl:startDate" />
End- <span jwcid="@DatePicker" date="ognl:endDate" />
..
~~~~~~~~~~~~
FooPage.java
~~~~~~~~~~~~
..
public abstract Date getStartDate();
public abstract void setStartDate(Date value);
public abstract Date getEndDate();
public abstract void setEndDate(Date value);
..
public void submitAction() {
Date d = getStartDate(); // THIS SHOULD BE VALID HERE - no work
needed by caller FooPage
}
..
~~~~~~~~~~~~
DatePicker.jwc
~~~~~~~~~~~~~~
..
<parameter name="date" required="yes" />
..
~~~~~~~~~~~~~~
DatePicker.java
~~~~~~~~~~~~~~
* don't want to use Listener -- it could be called out of order
* don't want to use renderComponent() -- i'd rather just write the
html than bury the html *in* java
* .. ???
what's the trick ?
~~~~~~~~~~~~~~
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Reusable Component - which handles FORM/REQUEST values
Posted by "B.S.Navin" <bs...@effigent.net>.
In your case, you want a method to be called after Tapestry calls
setDay(), setYear() and setMonth() during rewind.
You can do it in the cleanupAfterRender() method (As Daniel
suggested), or you can use the http://tapestry.apache.org/tapestry4/
tapestry/ComponentReference/InvokeListener.html component (sorry, I
was not clear last time). Here's an example...
Say this is your components template -
******
<input type="text" jwcid="dayField@TextField" value="..."/>
<input type="text" jwcid="monthField@TextField" value="..."/>
<input type="text" jwcid="yearField@TextField" value="..."/>
<span jwcid="@InvokeListener" listener="listener:calculateDate"/>
******
As you can see, I have added an InvokeListener component at the end.
So when Tapestry renders/rewinds, it will render/rewind each of the 3
textfields and then call the listener "calculateDate". In this
listener, you can check for isRewinding() and calculate the date and
set it into the parameter.
- Navin
On 25-Oct-06, at 10:45 AM, Matt Kerr wrote:
> Navin -
>
> Thanks very much for the reply.
>
>> You can use templates for components too (similar to page
>> templates), by extending the component class from "BaseComponent".
>> Then you just add an HTML with the same name as the component and
>> that's all.
>
> Sorry if original post is confusing.
>
> I'm ok with using templates on components (BaseComponent, etc), I
> think.
>
> Except for a specific case where I'm not sure when/how the
> component would perform it's primary task.
>
> An example of this might be a "Date Chooser" component -- where the
> generated HTML is 3 popups for YYYY / MM / DD .. and the caller
> binding passes in/out a Date object. eg,
>
> FooPage.html
> ~~~~~~~~~~~~~~~~
> ..
> <span jwcid="@DateChooser" date="startDate" />
> ..
> <span jwcid="@DateChooser" date="endDate" />
> ..
> ~~~~~~~~~~~~~~~~
>
> If I pretend I'm the DateChooser component, and the application
> accepts a form submit from FooPage..
>
> My question is:
>
> When do I call this.setDate() ?
>
> I don't know what guarantees the behavior of DateInput.java
> (below). Maybe that's what I'm missing? Something simple like
> call setDate() (or equivalent) in the set'er of the last form bound
> ivar(?), eg. DateInput.setYear(). I suppose that seems kind of
> cheezy vs. a notification or something more formal .. which is why
> I'd be surprised if that was the trick, but I could definitely work
> with that.
>
> If I don't call ~setDate() in the last set'er for the last input
> bound ivar .. then when is the appropriate time to call it?
>
> I don't have the app in front of me at the minute .. :-/
>
> But maybe something like PageEndRenderListener ? by implementing
> pageEndRender() - and checking for isRewinding() ?
>
> http://tapestry.apache.org/tapestry4/tapestry/apidocs/org/apache/
> tapestry/event/PageEndRenderListener.html
>
>
>
>> And you can use the "InvokeListener" component part of the core
>> Tapestry framework. Just add this component in your custom
>> component's template at the point where you want the listener
>> invoked. The parameters for this component are very much similar
>> to the DirectLink component. I think this will serve your purpose.
>
> Sorry - I wasn't more clear about 'listener' -doh!
> eg. - ?
>
> <form jwcid="@shared:SimpleForm"
> listener="ognl:listeners.castBallotAction" stateful="ognl:false"
> focus="ognl:false">
>
> I think you're just reminding me about form's listener &
> the .java's action method .. eg. castBallotAction() .. I guess
> what's important here is that the "Date Chooser" call setDate()
> before the action method is invoked. (Or better - at what point is
> the DateChooser.date available to the Page ?)
>
> I hope this is more clear.
>
> Ok, Thanks-Matt
>
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> package ws.sundraw.tapestry.components;
>
> import java.util.Calendar;
>
> import org.apache.tapestry.BaseComponent;
> import org.apache.tapestry.IBinding;
> import org.apache.tapestry.form.IPropertySelectionModel;
>
> /**
> * Implements a component that accepts date input and is rendered
> * as three HTML <select> elements, for day, month and year.
> * Months are shown by their names (January, February etc.) and are
> * automatically localized according to the preferred locale of
> the page
> * where the component is used
> *
> * @author Alexander Kolesnikov (www.sundraw.ws)
> * @version 1.0
> *
> **/
>
> public abstract class DateInput extends BaseComponent {
>
> private int d, m, y;
> private Calendar c;
>
> protected void initialize() {
> d = m = 1;
> y = 2000;
> c = null;
> }
>
> public IPropertySelectionModel getDaysModel() {
> return new DayModel();
> }
>
> public IPropertySelectionModel getMonthsModel() {
> return new LocalizedMonthsModel(getPage().getLocale());
> }
>
> public IPropertySelectionModel getYearsModel() {
> return new YearModel();
> }
>
> public int getDay() {
> c = readDate();
> d = c.get(Calendar.DATE);
> return d;
> }
>
> public void setDay(int day) {
> d = day;
> }
>
> public int getMonth() {
> m = c.get(Calendar.MONTH) + 1;
> return m;
> }
>
> public void setMonth(int month) {
> m = month;
> }
>
> public int getYear() {
> y = c.get(Calendar.YEAR);
> return y;
> }
>
> public void setYear(int year) {
> y = year;
> c.set(y, m - 1, d);
> upDate(c);
> }
>
> public Calendar readDate() {
> return (Calendar)getDateBinding().getObject("date", Calendar.class);
> }
>
> public void upDate(Calendar c) {
> getDateBinding().setObject(c);
> }
>
> public abstract IBinding getDateBinding();
>
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> ---------------------------------------------------------------------
> 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: Reusable Component - which handles FORM/REQUEST values
Posted by Matt Kerr <ma...@centralparksoftware.com>.
Navin -
Thanks very much for the reply.
> You can use templates for components too (similar to page
> templates), by extending the component class from "BaseComponent".
> Then you just add an HTML with the same name as the component and
> that's all.
Sorry if original post is confusing.
I'm ok with using templates on components (BaseComponent, etc), I think.
Except for a specific case where I'm not sure when/how the component
would perform it's primary task.
An example of this might be a "Date Chooser" component -- where the
generated HTML is 3 popups for YYYY / MM / DD .. and the caller
binding passes in/out a Date object. eg,
FooPage.html
~~~~~~~~~~~~~~~~
..
<span jwcid="@DateChooser" date="startDate" />
..
<span jwcid="@DateChooser" date="endDate" />
..
~~~~~~~~~~~~~~~~
If I pretend I'm the DateChooser component, and the application
accepts a form submit from FooPage..
My question is:
When do I call this.setDate() ?
I don't know what guarantees the behavior of DateInput.java (below).
Maybe that's what I'm missing? Something simple like call setDate()
(or equivalent) in the set'er of the last form bound ivar(?), eg.
DateInput.setYear(). I suppose that seems kind of cheezy vs. a
notification or something more formal .. which is why I'd be
surprised if that was the trick, but I could definitely work with that.
If I don't call ~setDate() in the last set'er for the last input
bound ivar .. then when is the appropriate time to call it?
I don't have the app in front of me at the minute .. :-/
But maybe something like PageEndRenderListener ? by implementing
pageEndRender() - and checking for isRewinding() ?
http://tapestry.apache.org/tapestry4/tapestry/apidocs/org/apache/
tapestry/event/PageEndRenderListener.html
> And you can use the "InvokeListener" component part of the core
> Tapestry framework. Just add this component in your custom
> component's template at the point where you want the listener
> invoked. The parameters for this component are very much similar to
> the DirectLink component. I think this will serve your purpose.
Sorry - I wasn't more clear about 'listener' -doh!
eg. - ?
<form jwcid="@shared:SimpleForm"
listener="ognl:listeners.castBallotAction" stateful="ognl:false"
focus="ognl:false">
I think you're just reminding me about form's listener & the .java's
action method .. eg. castBallotAction() .. I guess what's important
here is that the "Date Chooser" call setDate() before the action
method is invoked. (Or better - at what point is the
DateChooser.date available to the Page ?)
I hope this is more clear.
Ok, Thanks-Matt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package ws.sundraw.tapestry.components;
import java.util.Calendar;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IBinding;
import org.apache.tapestry.form.IPropertySelectionModel;
/**
* Implements a component that accepts date input and is rendered
* as three HTML <select> elements, for day, month and year.
* Months are shown by their names (January, February etc.) and are
* automatically localized according to the preferred locale of the
page
* where the component is used
*
* @author Alexander Kolesnikov (www.sundraw.ws)
* @version 1.0
*
**/
public abstract class DateInput extends BaseComponent {
private int d, m, y;
private Calendar c;
protected void initialize() {
d = m = 1;
y = 2000;
c = null;
}
public IPropertySelectionModel getDaysModel() {
return new DayModel();
}
public IPropertySelectionModel getMonthsModel() {
return new LocalizedMonthsModel(getPage().getLocale());
}
public IPropertySelectionModel getYearsModel() {
return new YearModel();
}
public int getDay() {
c = readDate();
d = c.get(Calendar.DATE);
return d;
}
public void setDay(int day) {
d = day;
}
public int getMonth() {
m = c.get(Calendar.MONTH) + 1;
return m;
}
public void setMonth(int month) {
m = month;
}
public int getYear() {
y = c.get(Calendar.YEAR);
return y;
}
public void setYear(int year) {
y = year;
c.set(y, m - 1, d);
upDate(c);
}
public Calendar readDate() {
return (Calendar)getDateBinding().getObject("date", Calendar.class);
}
public void upDate(Calendar c) {
getDateBinding().setObject(c);
}
public abstract IBinding getDateBinding();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Reusable Component - which handles FORM/REQUEST values
Posted by "B.S.Navin" <bs...@effigent.net>.
Hi Matt,
You can use templates for components too (similar to page templates),
by extending the component class from "BaseComponent". Then you just
add an HTML with the same name as the component and that's all.
And you can use the "InvokeListener" component part of the core
Tapestry framework. Just add this component in your custom
component's template at the point where you want the listener
invoked. The parameters for this component are very much similar to
the DirectLink component. I think this will serve your purpose.
- Navin
You can look at the InvokeListener component
On 25-Oct-06, at 5:16 AM, Daniel Tabuenca wrote:
> If I understand correctly, you're question is where to ultimately put
> the upDate(c);, In your example you put it on the setYear() but that
> relies on setYear being called last which I don't think is a
> guarantee. Alternatively you could put it on all of them (but this
> would be inneficient). I believ you can also override
>
> protected void cleanupAfterRender(IRequestCycle cycle);
>
> from the AbstractComponent class. I believe this is called after the
> render so all properties should have been set and you can save the
> date binding. You might also be able to do it through overriding
> finishLoad() or enterActivState(). But I am not sure if by that time
> the request has already been rewound.
>
>
>
>
> On 10/24/06, Matt Kerr <ma...@centralparksoftware.com> wrote:
>> I find Tapestry ok for generating html, but I'm struggling on the
>> request processing. I don't think I'm alone .. I see many references
>> to this topic on the mailing list archive -- and it's not very well
>> described in the documentation :-?
>>
>> Specifically, reusable components participating in request
>> processing.
>>
>> How can my component gather information in the request, and return an
>> object to its caller (eg. the page, or another component). I'm
>> looking at the point in time *after* my component has received it's
>> request values, but *before* the page has invoked the listener
>> action.
>>
>> Also, I'd like to use an html template .. (What's the point in using
>> Tapestry if I'm writing my html *in the Java*??)
>> ~~~
>> public void renderComponent(IMarkupWriter writer, IRequestCycle
>> cycle)
>> {
>> writer.begin("a");
>> writer.attribute("href", . . .);
>> writer.attribute("title", getTitle());
>> ~~
>>
>>
>>
>> I'm imagining something like .. FooPage and DatePicker component --
>> where the DatePicker hands a Date object to the page. A Date object
>> which the DatePicker creates from the FORM/request values sent to the
>> app. eg. ..
>>
>> http://host:port/foo/blah?year=2006&month=10&day=24
>>
>>
>> What does DatePicker.java implementation look like ??
>>
>> Can I use a template ? (Or do I have to write the html *in* the java
>> with IMarkupWriter, etc??)
>>
>> Is there a callback for the point in time I mention above? .. where
>> the component is filled in by the framework, and it has an
>> opportunity to perform it's purpose, and then processing continues
>> elsewhere--like in the caller.
>>
>> I believe "listener" callback isn't an option because you're not
>> guaranteed of the order.
>>
>> The examples I'm reading online all break encapsulation and just have
>> the caller get the M/D/Y from the component so that the *caller*
>> creates the date. That's completely wrong -- and defeats the purpose
>> of a "reusable component" .. since the caller is doing the work.
>>
>> Any ideas greatly appreciated.
>>
>> My sketch goes something like this (below). I'm really wondering
>> what DatePicker.java does to push a value back to the caller *before*
>> the caller's action() method is called.
>>
>> Thanks,
>> Matt
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> FooPage.html
>> ~~~~~~~~~~~~
>> ..
>> Start- <span jwcid="@DatePicker" date="ognl:startDate" />
>> End- <span jwcid="@DatePicker" date="ognl:endDate" />
>> ..
>> ~~~~~~~~~~~~
>>
>> FooPage.java
>> ~~~~~~~~~~~~
>> ..
>> public abstract Date getStartDate();
>> public abstract void setStartDate(Date value);
>> public abstract Date getEndDate();
>> public abstract void setEndDate(Date value);
>> ..
>> public void submitAction() {
>> Date d = getStartDate(); // THIS SHOULD BE VALID
>> HERE - no work
>> needed by caller FooPage
>> }
>> ..
>> ~~~~~~~~~~~~
>>
>>
>> DatePicker.jwc
>> ~~~~~~~~~~~~~~
>> ..
>> <parameter name="date" required="yes" />
>> ..
>> ~~~~~~~~~~~~~~
>>
>> DatePicker.java
>> ~~~~~~~~~~~~~~
>> * don't want to use Listener -- it could be called out of
>> order
>> * don't want to use renderComponent() -- i'd rather just
>> write the
>> html than bury the html *in* java
>> * .. ???
>>
>> what's the trick ?
>> ~~~~~~~~~~~~~~
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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: Reusable Component - which handles FORM/REQUEST values
Posted by Matt Kerr <ma...@centralparksoftware.com>.
Daniel -
> If I understand correctly, you're question is where to ultimately put
> the upDate(c);, In your example you put it on the setYear() but that
> relies on setYear being called last which I don't think is a
> guarantee. Alternatively you could put it on all of them (but this
> would be inneficient).
Yes - exactly. Definitely don't want to solve it like that.
> I believ you can also override
>
> protected void cleanupAfterRender(IRequestCycle cycle);
>
> from the AbstractComponent class. I believe this is called after the
> render so all properties should have been set and you can save the
> date binding. You might also be able to do it through overriding
> finishLoad() or enterActiveState(). But I am not sure if by that time
> the request has already been rewound.
Thanks for the tip - I'll check out cleanupAfterRender() .. and the
others tomorrow.
Thank you list members,-Matt
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Reusable Component - which handles FORM/REQUEST values
Posted by Daniel Tabuenca <dt...@gmail.com>.
If I understand correctly, you're question is where to ultimately put
the upDate(c);, In your example you put it on the setYear() but that
relies on setYear being called last which I don't think is a
guarantee. Alternatively you could put it on all of them (but this
would be inneficient). I believ you can also override
protected void cleanupAfterRender(IRequestCycle cycle);
from the AbstractComponent class. I believe this is called after the
render so all properties should have been set and you can save the
date binding. You might also be able to do it through overriding
finishLoad() or enterActivState(). But I am not sure if by that time
the request has already been rewound.
On 10/24/06, Matt Kerr <ma...@centralparksoftware.com> wrote:
> I find Tapestry ok for generating html, but I'm struggling on the
> request processing. I don't think I'm alone .. I see many references
> to this topic on the mailing list archive -- and it's not very well
> described in the documentation :-?
>
> Specifically, reusable components participating in request processing.
>
> How can my component gather information in the request, and return an
> object to its caller (eg. the page, or another component). I'm
> looking at the point in time *after* my component has received it's
> request values, but *before* the page has invoked the listener action.
>
> Also, I'd like to use an html template .. (What's the point in using
> Tapestry if I'm writing my html *in the Java*??)
> ~~~
> public void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
> {
> writer.begin("a");
> writer.attribute("href", . . .);
> writer.attribute("title", getTitle());
> ~~
>
>
>
> I'm imagining something like .. FooPage and DatePicker component --
> where the DatePicker hands a Date object to the page. A Date object
> which the DatePicker creates from the FORM/request values sent to the
> app. eg. ..
>
> http://host:port/foo/blah?year=2006&month=10&day=24
>
>
> What does DatePicker.java implementation look like ??
>
> Can I use a template ? (Or do I have to write the html *in* the java
> with IMarkupWriter, etc??)
>
> Is there a callback for the point in time I mention above? .. where
> the component is filled in by the framework, and it has an
> opportunity to perform it's purpose, and then processing continues
> elsewhere--like in the caller.
>
> I believe "listener" callback isn't an option because you're not
> guaranteed of the order.
>
> The examples I'm reading online all break encapsulation and just have
> the caller get the M/D/Y from the component so that the *caller*
> creates the date. That's completely wrong -- and defeats the purpose
> of a "reusable component" .. since the caller is doing the work.
>
> Any ideas greatly appreciated.
>
> My sketch goes something like this (below). I'm really wondering
> what DatePicker.java does to push a value back to the caller *before*
> the caller's action() method is called.
>
> Thanks,
> Matt
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> FooPage.html
> ~~~~~~~~~~~~
> ..
> Start- <span jwcid="@DatePicker" date="ognl:startDate" />
> End- <span jwcid="@DatePicker" date="ognl:endDate" />
> ..
> ~~~~~~~~~~~~
>
> FooPage.java
> ~~~~~~~~~~~~
> ..
> public abstract Date getStartDate();
> public abstract void setStartDate(Date value);
> public abstract Date getEndDate();
> public abstract void setEndDate(Date value);
> ..
> public void submitAction() {
> Date d = getStartDate(); // THIS SHOULD BE VALID HERE - no work
> needed by caller FooPage
> }
> ..
> ~~~~~~~~~~~~
>
>
> DatePicker.jwc
> ~~~~~~~~~~~~~~
> ..
> <parameter name="date" required="yes" />
> ..
> ~~~~~~~~~~~~~~
>
> DatePicker.java
> ~~~~~~~~~~~~~~
> * don't want to use Listener -- it could be called out of order
> * don't want to use renderComponent() -- i'd rather just write the
> html than bury the html *in* java
> * .. ???
>
> what's the trick ?
> ~~~~~~~~~~~~~~
>
>
>
>
>
> ---------------------------------------------------------------------
> 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