You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Martin Dietze <di...@fh-wedel.de> on 2007/05/30 15:37:26 UTC

T5: when is @Persist necessary and when not...

Hi,

 I have a page class which keeps form data like this:

| // class MyForm
| private Person _person;
| private String _homepage;

The class Person consists of several fields:

| // class Person
| private String _firstName
| private String _lastName

For all these I have getters and setters defined.

In my page class I have a constructor like this:

| public CreatePartner() {
|   _contact = new Person();
| }

When I submit data in my form all the above gets written
to the database as expected. However the textinput referring
to the _homepage member variable is always empty.

Now if I annotate the _homepage member variable using
@Persist("flash"), I get the data displayed as expected.
This seems to indicate that simple objects are somehow
treated differently from the way compound objects are
treated?

Strictly speaking I would not expect to need any persistence
annotation here at all since I am only handling submitted
data, or do I misunderstand anything here?

Cheers,

Martin

P.S.: These is the relevant entries in my form:

| <input type="text" name="firstname" id="firstname" tabindex="20"
|     t:type="textfield"
|     t:id="firstname"
|     t:value="contact.firstname"
|     t:validate="required,minlength=3"
|     />

[...]

| <input type="text" name="lastname" id="lastname" tabindex="20"
|     t:type="textfield"
|     t:id="lastname"
|     t:value="contact.lastname"
|     t:validate="required,minlength=3"
|     />

[...]

| <input type="text" name="homepage" id="homepage" tabindex="40"
|     t:type="textfield"
|     t:id="homepage"
|     t:value="homepage"
|     t:validate="required,minlength=3"
|     />


Martin

-- 
-------------- Martin Dietze --------------- / ------ freiheit.com ----------
-- martin@the-little-red-haired-girl.org -- / -- martin.dietze@freiheit.com -
------------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
Last yeer I kudn't spel Engineer.  Now I are won.

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


RE: T5: when is @Persist necessary and when not...

Posted by Ben Sommerville <be...@bulletproof.com.au>.
If you initialise the objects in either of those methods then 
you shouldn't need default values anymore.  
i.e no assignment in constructor or field init

Otherwise if you are @Persisting those variables in the page 
(not the compound object) then you values should be retained.

If you are doing all that & it isn't working I think I'd need
to see the code again to be able help any further

cheers,
Ben

> -----Original Message-----
> From: Martin Dietze [mailto:di@mail.fh-wedel.de] On Behalf Of 
> Martin Dietze
> Sent: Friday, 1 June 2007 5:33 PM
> To: 'Tapestry users'
> Subject: Re: T5: when is @Persist necessary and when not...
> 
> On Thu, May 31, 2007, Ben Sommerville wrote:
> 
> > For a form I'd say the best place to create those compound objects
> > is in the onPrepare method.  This will be invoked before 
> any processing/
> > rendering occurs.  (see the Form component subsection of 
> > 
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/valid
> ation.html)
> > 
> > Another option is the pageAttached method (invoked when page
> > is attached to the request) which is documented at
> > 
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/lifec
> ycle.html
> 
> Hmm, having tried both these options this leads to the objects
> being replaced by defaults before the resultpage after a submit
> is created, thus my submit results (the variables are annotated
> with "@Persist") get lost. Or am I misunderstanding something
> completely?
> 
> Cheers,
> 
> Martin
> 
> -- 
> ----------- / http://herbert.the-little-red-haired-girl.org / 
> -------------
> =+= 
> Wie verhindert man, dass einem die Geige gestohlen wird?
> Man legt sie in einen Bratschenkasten...
> 
> ---------------------------------------------------------------------
> 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: T5: when is @Persist necessary and when not...

Posted by Martin Dietze <di...@fh-wedel.de>.
On Thu, May 31, 2007, Ben Sommerville wrote:

> For a form I'd say the best place to create those compound objects
> is in the onPrepare method.  This will be invoked before any processing/
> rendering occurs.  (see the Form component subsection of 
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html)
> 
> Another option is the pageAttached method (invoked when page
> is attached to the request) which is documented at
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/lifecycle.html

Hmm, having tried both these options this leads to the objects
being replaced by defaults before the resultpage after a submit
is created, thus my submit results (the variables are annotated
with "@Persist") get lost. Or am I misunderstanding something
completely?

Cheers,

Martin

-- 
----------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
Wie verhindert man, dass einem die Geige gestohlen wird?
Man legt sie in einen Bratschenkasten...

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


RE: T5: when is @Persist necessary and when not...

Posted by Ben Sommerville <be...@bulletproof.com.au>.
The default value that the _person member gets reset to will be 
the Person _instance_ that you assigned in the constructor.

So when the form is submitted that instance will have its members
set based on the form values.  When the page is rendered the 
same instance will be used as the default value & you will see
your submitted values.

Basically default values only work properly when they are immutable.
e.g. strings, 

For a form I'd say the best place to create those compound objects
is in the onPrepare method.  This will be invoked before any processing/
rendering occurs.  (see the Form component subsection of 
http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html)

Another option is the pageAttached method (invoked when page
is attached to the request) which is documented at
http://tapestry.apache.org/tapestry5/tapestry-core/guide/lifecycle.html

cheers,
Ben


> 
> This is strange. If the _person member variable gets reset to
> the default at the end of each request I would expect not to
> see the values from it on my page after submit in the render
> request.
> 
> What I want to achieve is very much the same scenario found in
> the documentation section "Form Input and Validation", only I
> have several compound objects like `Person'. Since the form
> accesses members of these objects they need to be created in
> the page class. Since the constructor is obviously not the
> right place for this, which one would you suggest?
> 
> Cheers,
> 
> Martin
> 
> -- 
> ----------- / http://herbert.the-little-red-haired-girl.org / 
> -------------
> =+= 
> Was ist ein Cluster?
> Wenn vier Bratscher unisono spielen.
> 
> ---------------------------------------------------------------------
> 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: T5: when is @Persist necessary and when not...

Posted by Martin Dietze <di...@fh-wedel.de>.
On Wed, May 30, 2007, Howard Lewis Ship wrote:

> >On 5/30/07, Martin Dietze < di@fh-wedel.de> wrote:

[...]

> >> > >For all these I have getters and setters defined.
> >> > >
> >> > >In my page class I have a constructor like this:
> >> > >
> >> > >| public CreatePartner() {
> >> > >|   _contact = new Person();
> >>         ^^^^^^^ `person' of course!
> >> > >| }

[...]

> Just noticed your constructor. Don't do that.
> 
> The Person object that you create will become the default value for this
> property, rather than null.  This is documented.

This is strange. If the _person member variable gets reset to
the default at the end of each request I would expect not to
see the values from it on my page after submit in the render
request.

What I want to achieve is very much the same scenario found in
the documentation section "Form Input and Validation", only I
have several compound objects like `Person'. Since the form
accesses members of these objects they need to be created in
the page class. Since the constructor is obviously not the
right place for this, which one would you suggest?

Cheers,

Martin

-- 
----------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
Was ist ein Cluster?
Wenn vier Bratscher unisono spielen.

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


Re: T5: when is @Persist necessary and when not...

Posted by Howard Lewis Ship <hl...@gmail.com>.
Just noticed your constructor. Don't do that.

The Person object that you create will become the default value for this
property, rather than null.  This is documented.

On 5/30/07, Howard Lewis Ship <hl...@gmail.com> wrote:
>
> There should not be any difference; I'd have to see your code to determine
> what's going on.
>
> On 5/30/07, Martin Dietze < di@fh-wedel.de> wrote:
> >
> > On Wed, May 30, 2007, Howard Lewis Ship wrote:
> >
> > > >I have a page class which keeps form data like this:
> > > >
> > > >| // class MyForm
> > > >| private Person _person;
> > > >| private String _homepage;
> > > >
> > > >The class Person consists of several fields:
> > > >
> > > >| // class Person
> > > >| private String _firstName
> > > >| private String _lastName
> > > >
> > > >For all these I have getters and setters defined.
> > > >
> > > >In my page class I have a constructor like this:
> > > >
> > > >| public CreatePartner() {
> > > >|   _contact = new Person();
> >         ^^^^^^^ `person' of course!
> > > >| }
> >
> > [...]
> >
> > > Tapestry uses a redirect-after-post for actions, including form
> > submits.
> > > Thus if the data is stored persistently, usually in the HttpSession,
> > it is
> > > gone on the second request, the one that actually renders a response.
> >
> > > If you used the debugger, you'd see that your _person field would also
> > be
> > > null in the second request, the render request. You just aren't using
> > it.
> >
> > Interestingly this does not seem to be the case in my
> > application. Debugging the above class I find that when the
> > page is rendered after submit the `Person' object still
> > contains the submitted values.
> >
> > Additionally `_$person_default' object (as I guess created by
> > T5) contains the values of the populated person object.
> >
> > For the `_homeapage' object which is an ordinary String, the
> > values are lost unless I put a `@Persist("flash")' above it
> > which should be expected according to your post. Now I wonder
> > why does the Person object seem to behave differently?
> >
> > Cheers,
> >
> > Martin
> >
> > --
> > ------------- / http://herbert.the-little-red-haired-girl.org /
> > -------------
> > =+=
> > The only "intuitive" interface is the nipple. After that, it's all
> > learned.  -- Bruce Ediger
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
>
> --
> Howard M. Lewis Ship
> TWD Consulting, Inc.
> Independent J2EE / Open-Source Java Consultant
> Creator and PMC Chair, Apache Tapestry
> Creator, Apache HiveMind
>
> Professional Tapestry training, mentoring, support
> and project work.   http://howardlewisship.com
>



-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

Re: T5: when is @Persist necessary and when not...

Posted by Howard Lewis Ship <hl...@gmail.com>.
There should not be any difference; I'd have to see your code to determine
what's going on.

On 5/30/07, Martin Dietze <di...@fh-wedel.de> wrote:
>
> On Wed, May 30, 2007, Howard Lewis Ship wrote:
>
> > >I have a page class which keeps form data like this:
> > >
> > >| // class MyForm
> > >| private Person _person;
> > >| private String _homepage;
> > >
> > >The class Person consists of several fields:
> > >
> > >| // class Person
> > >| private String _firstName
> > >| private String _lastName
> > >
> > >For all these I have getters and setters defined.
> > >
> > >In my page class I have a constructor like this:
> > >
> > >| public CreatePartner() {
> > >|   _contact = new Person();
>         ^^^^^^^ `person' of course!
> > >| }
>
> [...]
>
> > Tapestry uses a redirect-after-post for actions, including form submits.
> > Thus if the data is stored persistently, usually in the HttpSession, it
> is
> > gone on the second request, the one that actually renders a response.
>
> > If you used the debugger, you'd see that your _person field would also
> be
> > null in the second request, the render request. You just aren't using
> it.
>
> Interestingly this does not seem to be the case in my
> application. Debugging the above class I find that when the
> page is rendered after submit the `Person' object still
> contains the submitted values.
>
> Additionally `_$person_default' object (as I guess created by
> T5) contains the values of the populated person object.
>
> For the `_homeapage' object which is an ordinary String, the
> values are lost unless I put a `@Persist("flash")' above it
> which should be expected according to your post. Now I wonder
> why does the Person object seem to behave differently?
>
> Cheers,
>
> Martin
>
> --
> ------------- / http://herbert.the-little-red-haired-girl.org /
> -------------
> =+=
> The only "intuitive" interface is the nipple. After that, it's all
> learned.  -- Bruce Ediger
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

Re: T5: when is @Persist necessary and when not...

Posted by Martin Dietze <di...@fh-wedel.de>.
On Wed, May 30, 2007, Howard Lewis Ship wrote:

> >I have a page class which keeps form data like this:
> >
> >| // class MyForm
> >| private Person _person;
> >| private String _homepage;
> >
> >The class Person consists of several fields:
> >
> >| // class Person
> >| private String _firstName
> >| private String _lastName
> >
> >For all these I have getters and setters defined.
> >
> >In my page class I have a constructor like this:
> >
> >| public CreatePartner() {
> >|   _contact = new Person();
        ^^^^^^^ `person' of course!
> >| }

[...]

> Tapestry uses a redirect-after-post for actions, including form submits.
> Thus if the data is stored persistently, usually in the HttpSession, it is
> gone on the second request, the one that actually renders a response.

> If you used the debugger, you'd see that your _person field would also be
> null in the second request, the render request. You just aren't using it.

Interestingly this does not seem to be the case in my
application. Debugging the above class I find that when the
page is rendered after submit the `Person' object still
contains the submitted values.

Additionally `_$person_default' object (as I guess created by 
T5) contains the values of the populated person object.

For the `_homeapage' object which is an ordinary String, the
values are lost unless I put a `@Persist("flash")' above it
which should be expected according to your post. Now I wonder
why does the Person object seem to behave differently?

Cheers,

Martin

-- 
------------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
The only "intuitive" interface is the nipple. After that, it's all
learned.  -- Bruce Ediger 

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


Re: T5: when is @Persist necessary and when not...

Posted by Howard Lewis Ship <hl...@gmail.com>.
@Persist isn't about database persistence; it is about a value persisting
from one request to the next, or beyond.

Tapestry uses a redirect-after-post for actions, including form submits.
Thus if the data is stored persistently, usually in the HttpSession, it is
gone on the second request, the one that actually renders a response.

If you used the debugger, you'd see that your _person field would also be
null in the second request, the render request. You just aren't using it.

On 5/30/07, Martin Dietze <di...@fh-wedel.de> wrote:
>
> Hi,
>
> I have a page class which keeps form data like this:
>
> | // class MyForm
> | private Person _person;
> | private String _homepage;
>
> The class Person consists of several fields:
>
> | // class Person
> | private String _firstName
> | private String _lastName
>
> For all these I have getters and setters defined.
>
> In my page class I have a constructor like this:
>
> | public CreatePartner() {
> |   _contact = new Person();
> | }
>
> When I submit data in my form all the above gets written
> to the database as expected. However the textinput referring
> to the _homepage member variable is always empty.
>
> Now if I annotate the _homepage member variable using
> @Persist("flash"), I get the data displayed as expected.
> This seems to indicate that simple objects are somehow
> treated differently from the way compound objects are
> treated?
>
> Strictly speaking I would not expect to need any persistence
> annotation here at all since I am only handling submitted
> data, or do I misunderstand anything here?
>
> Cheers,
>
> Martin
>
> P.S.: These is the relevant entries in my form:
>
> | <input type="text" name="firstname" id="firstname" tabindex="20"
> |     t:type="textfield"
> |     t:id="firstname"
> |     t:value="contact.firstname"
> |     t:validate="required,minlength=3"
> |     />
>
> [...]
>
> | <input type="text" name="lastname" id="lastname" tabindex="20"
> |     t:type="textfield"
> |     t:id="lastname"
> |     t:value="contact.lastname"
> |     t:validate="required,minlength=3"
> |     />
>
> [...]
>
> | <input type="text" name="homepage" id="homepage" tabindex="40"
> |     t:type="textfield"
> |     t:id="homepage"
> |     t:value="homepage"
> |     t:validate="required,minlength=3"
> |     />
>
>
> Martin
>
> --
> -------------- Martin Dietze --------------- / ------ freiheit.com----------
> -- martin@the-little-red-haired-girl.org -- / --
> martin.dietze@freiheit.com -
> ------------- / http://herbert.the-little-red-haired-girl.org /
> -------------
> =+=
> Last yeer I kudn't spel Engineer.  Now I are won.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com