You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Chris Merrill <ch...@webperformance.com> on 2010/08/09 05:16:09 UTC

DataView/LoadableDetachableModel/onClick

I have a DataView on a page to list items returned by a DB query (using JPA).  My database object
(Customer) has LoadableDetachableModel class (called DetachableCustomerModel) that the provider
returns via the model() method.  When populating the table, I want a link to a page for the details
of that object. The following code actually works, but throws a WicketNotSerializableException
on my Customer class when the page is rendered:


        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
            {
            @Override
            protected void populateItem(final Item<Customer> item)
                {
                final Customer customer = item.getModelObject();
                item.add(new Label("id", String.valueOf(customer.getId())));
                Link link = new Link("link")
                    {
                    @Override
                    public void onClick()
                        {
                        Debug.log.out("CustomerListPage.onClick() - customer: " + customer.getName());
                        setResponsePage(new CustomerPage(customer));
                        }
                    };
                item.add(link);
                link.add(new Label("name", customer.getName()));
                String date_string = "";
                Date date = customer.getLastActivity();
                if (date != null)                         // TODO should this really ever be null?
                    date_string = DateUtil.toSimpleDateString(date);
                item.add(new Label("date", date_string));
                }
            };

>From what I understand, it SHOULD throw that exception, since the Customer came from JPA and
shouldn't be serialized. That is, I think, why the LoadableDetachableModel exists.  So, thinking
that I should be using the model instead, I tried this variation, but was surprised to find
the same result.

        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
            {
            @Override
            protected void populateItem(final Item<Customer> item)
                {
                final IModel<Customer> customer_model = item.getModel();
                Customer customer = customer_model.getObject();
                item.add(new Label("id", String.valueOf(customer.getId())));
                Link link = new Link("link")
                    {
                    @Override
                    public void onClick()
                        {
                        Debug.log.out("CustomerListPage.onClick() - customer: " +
customer_model.getObject().getName());
                        setResponsePage(new CustomerPage(customer_model.getObject()));
                        }
                    };
                item.add(link);
                link.add(new Label("name", customer.getName()));
                String date_string = "";
                Date date = customer.getLastActivity();
                if (date != null)                         // TODO should this really ever be null?
                    date_string = DateUtil.toSimpleDateString(date);
                item.add(new Label("date", date_string));
                }
            };

So I'm obviously missing something important about the right way to do this.

Can someone point me in the right direction?

TIA!
Chris



-- 
------------------------------------------------------------------------ -
Chris Merrill                           |  Web Performance, Inc.
chris@webperformance.com                |  http://webperformance.com
919-433-1762                            |  919-845-7601

Web Performance: Website Load Testing Software & Services
------------------------------------------------------------------------ -

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Igor Vaynberg <ig...@gmail.com>.
there are provisions for serialization, but it is not perfect. eg if a
model alters a property of the page to which it carries the reference
those changes will be lost because what will be serialized is just a
"reference to page x".

-igor

On Tue, Aug 10, 2010 at 12:14 AM, Leszek Gawron <lg...@mobilebox.pl> wrote:
> On 2010-08-09 18:18, Igor Vaynberg wrote:
>>
>> it is not recommended to pass models between pages because if the
>> model is anonymous it can carry with it a reference to the other page
>> object and your session size will spike because your page also has a
>> reference to the previous page.
>>
>> so no, it shouldnt be that.
>
> that is why I use model classes which are nested private static and get the
> best out of both approaches.
>
> Still I thought that you have been debating on "smart serialization of
> anonymous models" and making them safe to use. Am I wrong?
>
>>
>> -igor
>>
>> On Mon, Aug 9, 2010 at 12:19 AM, Leszek Gawron<lg...@mobilebox.pl>
>>  wrote:
>>>
>>> On 2010-08-09 05:32, Igor Vaynberg wrote:
>>>>
>>>>  final Customer customer = item.getModelObject();
>>>> ...               Link link = new Link("link") {
>>>>                    public void onClick()                       {
>>>>                        setResponsePage(new CustomerPage(customer));
>>>>
>>>> the line above holds on to the customer object, so the Link subclass
>>>> has a reference to customer. instead
>>>> ...               Link link = new Link("link", item.getmodel()) {
>>>>                    public void onClick()                       {
>>>>
>>>>                        setResponsePage(new
>>>> CustomerPage((Customer)getmodelobject()));
>
>
> --
> Leszek Gawron                         http://www.mobilebox.pl/krs.html
> CTO at MobileBox Ltd.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Leszek Gawron <lg...@mobilebox.pl>.
On 2010-08-09 18:18, Igor Vaynberg wrote:
> it is not recommended to pass models between pages because if the
> model is anonymous it can carry with it a reference to the other page
> object and your session size will spike because your page also has a
> reference to the previous page.
>
> so no, it shouldnt be that.

that is why I use model classes which are nested private static and get 
the best out of both approaches.

Still I thought that you have been debating on "smart serialization of 
anonymous models" and making them safe to use. Am I wrong?

>
> -igor
>
> On Mon, Aug 9, 2010 at 12:19 AM, Leszek Gawron<lg...@mobilebox.pl>  wrote:
>> On 2010-08-09 05:32, Igor Vaynberg wrote:
>>>
>>>   final Customer customer = item.getModelObject();
>>> ...               Link link = new Link("link") {
>>>                     public void onClick()                       {
>>>                         setResponsePage(new CustomerPage(customer));
>>>
>>> the line above holds on to the customer object, so the Link subclass
>>> has a reference to customer. instead
>>> ...               Link link = new Link("link", item.getmodel()) {
>>>                     public void onClick()                       {
>>>
>>>                         setResponsePage(new
>>> CustomerPage((Customer)getmodelobject()));


-- 
Leszek Gawron                         http://www.mobilebox.pl/krs.html
CTO at MobileBox Ltd.

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Igor Vaynberg <ig...@gmail.com>.
it is not recommended to pass models between pages because if the
model is anonymous it can carry with it a reference to the other page
object and your session size will spike because your page also has a
reference to the previous page.

so no, it shouldnt be that.

-igor

On Mon, Aug 9, 2010 at 12:19 AM, Leszek Gawron <lg...@mobilebox.pl> wrote:
> On 2010-08-09 05:32, Igor Vaynberg wrote:
>>
>>  final Customer customer = item.getModelObject();
>> ...               Link link = new Link("link") {
>>                    public void onClick()                       {
>>                        setResponsePage(new CustomerPage(customer));
>>
>> the line above holds on to the customer object, so the Link subclass
>> has a reference to customer. instead
>> ...               Link link = new Link("link", item.getmodel()) {
>>                    public void onClick()                       {
>>
>>                        setResponsePage(new
>> CustomerPage((Customer)getmodelobject()));
>
> shouldn't it actually be :
>
> Link link = new Link( "link, item.getModel() ) {
>  public void onClick() {
>      setResponsePage( new CustomerPage( getModel() ) );
>  }
> }
>
> ?
>
> --
> Leszek Gawron                         http://www.mobilebox.pl/krs.html
> CTO at MobileBox Ltd.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Leszek Gawron <lg...@mobilebox.pl>.
On 2010-08-09 05:32, Igor Vaynberg wrote:
>   final Customer customer = item.getModelObject();
> ...               Link link = new Link("link") {
>                     public void onClick()                       {
>                         setResponsePage(new CustomerPage(customer));
>
> the line above holds on to the customer object, so the Link subclass
> has a reference to customer. instead
> ...               Link link = new Link("link", item.getmodel()) {
>                     public void onClick()                       {
>
>                         setResponsePage(new
> CustomerPage((Customer)getmodelobject()));

shouldn't it actually be :

Link link = new Link( "link, item.getModel() ) {
   public void onClick() {
       setResponsePage( new CustomerPage( getModel() ) );
   }
}

?

-- 
Leszek Gawron                         http://www.mobilebox.pl/krs.html
CTO at MobileBox Ltd.

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Chris Merrill <ch...@webperformance.com>.
I tried this, but still got the WicketNotSerializableException on the same class - the JPA enhanced
version of my Customer object (which is a very simple bean).  However, I tried making Customer
serializable and now the exception is gone.  I was thinking that I could not make Customer
serializable (or should not need to) because of the JPA connection and the use of
DetachableLoadableModel.  But then I had a vague memory that Models do need to be serializable?
I read all about Models more than a year ago when I first started looking into Wicket and thought
I understood them, but it's hazy now. I'm going to take a long slow read through
  https://cwiki.apache.org/WICKET/working-with-wicket-models.html
because I now remember how important Models are to getting more advanced stuff in Wicket working
the right way.  I'll be back after I've done my homework.

Was there a chapter in Wicket In Action on models?  I bought that a while back and seem to remember
a good Models chapter, but lost my e-copy.  Might have to buy it again!

Thanks for the help!
Chris


On 8/8/2010 11:32 PM, Igor Vaynberg wrote:
>  final Customer customer = item.getModelObject();
> ...               Link link = new Link("link") {
>                    public void onClick()                       {
>                        setResponsePage(new CustomerPage(customer));
> 
> the line above holds on to the customer object, so the Link subclass
> has a reference to customer. instead
> ...               Link link = new Link("link", item.getmodel()) {
>                    public void onClick()                       {
> 
>                        setResponsePage(new
> CustomerPage((Customer)getmodelobject()));
> 
> -igor
> 
> On Sun, Aug 8, 2010 at 8:16 PM, Chris Merrill <ch...@webperformance.com> wrote:
>> I have a DataView on a page to list items returned by a DB query (using JPA).  My database object
>> (Customer) has LoadableDetachableModel class (called DetachableCustomerModel) that the provider
>> returns via the model() method.  When populating the table, I want a link to a page for the details
>> of that object. The following code actually works, but throws a WicketNotSerializableException
>> on my Customer class when the page is rendered:
>>
>>
>>        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
>>            {
>>            @Override
>>            protected void populateItem(final Item<Customer> item)
>>                {
>>                final Customer customer = item.getModelObject();
>>                item.add(new Label("id", String.valueOf(customer.getId())));
>>                Link link = new Link("link")
>>                    {
>>                    @Override
>>                    public void onClick()
>>                        {
>>                        Debug.log.out("CustomerListPage.onClick() - customer: " + customer.getName());
>>                        setResponsePage(new CustomerPage(customer));
>>                        }
>>                    };
>>                item.add(link);
>>                link.add(new Label("name", customer.getName()));
>>                String date_string = "";
>>                Date date = customer.getLastActivity();
>>                if (date != null)                         // TODO should this really ever be null?
>>                    date_string = DateUtil.toSimpleDateString(date);
>>                item.add(new Label("date", date_string));
>>                }
>>            };
>>
>> From what I understand, it SHOULD throw that exception, since the Customer came from JPA and
>> shouldn't be serialized. That is, I think, why the LoadableDetachableModel exists.  So, thinking
>> that I should be using the model instead, I tried this variation, but was surprised to find
>> the same result.
>>
>>        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
>>            {
>>            @Override
>>            protected void populateItem(final Item<Customer> item)
>>                {
>>                final IModel<Customer> customer_model = item.getModel();
>>                Customer customer = customer_model.getObject();
>>                item.add(new Label("id", String.valueOf(customer.getId())));
>>                Link link = new Link("link")
>>                    {
>>                    @Override
>>                    public void onClick()
>>                        {
>>                        Debug.log.out("CustomerListPage.onClick() - customer: " +
>> customer_model.getObject().getName());
>>                        setResponsePage(new CustomerPage(customer_model.getObject()));
>>                        }
>>                    };
>>                item.add(link);
>>                link.add(new Label("name", customer.getName()));
>>                String date_string = "";
>>                Date date = customer.getLastActivity();
>>                if (date != null)                         // TODO should this really ever be null?
>>                    date_string = DateUtil.toSimpleDateString(date);
>>                item.add(new Label("date", date_string));
>>                }
>>            };
>>
>> So I'm obviously missing something important about the right way to do this.
>>
>> Can someone point me in the right direction?
>>
>> TIA!
>> Chris
>>
>>
>>
>> --
>> ------------------------------------------------------------------------ -
>> Chris Merrill                           |  Web Performance, Inc.
>> chris@webperformance.com                |  http://webperformance.com
>> 919-433-1762                            |  919-845-7601
>>
>> Web Performance: Website Load Testing Software & Services
>> ------------------------------------------------------------------------ -
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 


-- 
------------------------------------------------------------------------ -
Chris Merrill                           |  Web Performance, Inc.
chris@webperformance.com                |  http://webperformance.com
919-433-1762                            |  919-845-7601

Web Performance: Website Load Testing Software & Services
------------------------------------------------------------------------ -

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


Re: DataView/LoadableDetachableModel/onClick

Posted by Igor Vaynberg <ig...@gmail.com>.
 final Customer customer = item.getModelObject();
...               Link link = new Link("link") {
                   public void onClick()                       {
                       setResponsePage(new CustomerPage(customer));

the line above holds on to the customer object, so the Link subclass
has a reference to customer. instead
...               Link link = new Link("link", item.getmodel()) {
                   public void onClick()                       {

                       setResponsePage(new
CustomerPage((Customer)getmodelobject()));

-igor

On Sun, Aug 8, 2010 at 8:16 PM, Chris Merrill <ch...@webperformance.com> wrote:
> I have a DataView on a page to list items returned by a DB query (using JPA).  My database object
> (Customer) has LoadableDetachableModel class (called DetachableCustomerModel) that the provider
> returns via the model() method.  When populating the table, I want a link to a page for the details
> of that object. The following code actually works, but throws a WicketNotSerializableException
> on my Customer class when the page is rendered:
>
>
>        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
>            {
>            @Override
>            protected void populateItem(final Item<Customer> item)
>                {
>                final Customer customer = item.getModelObject();
>                item.add(new Label("id", String.valueOf(customer.getId())));
>                Link link = new Link("link")
>                    {
>                    @Override
>                    public void onClick()
>                        {
>                        Debug.log.out("CustomerListPage.onClick() - customer: " + customer.getName());
>                        setResponsePage(new CustomerPage(customer));
>                        }
>                    };
>                item.add(link);
>                link.add(new Label("name", customer.getName()));
>                String date_string = "";
>                Date date = customer.getLastActivity();
>                if (date != null)                         // TODO should this really ever be null?
>                    date_string = DateUtil.toSimpleDateString(date);
>                item.add(new Label("date", date_string));
>                }
>            };
>
> From what I understand, it SHOULD throw that exception, since the Customer came from JPA and
> shouldn't be serialized. That is, I think, why the LoadableDetachableModel exists.  So, thinking
> that I should be using the model instead, I tried this variation, but was surprised to find
> the same result.
>
>        final DataView<Customer> table_viewer = new DataView<Customer>("customer_list", provider)
>            {
>            @Override
>            protected void populateItem(final Item<Customer> item)
>                {
>                final IModel<Customer> customer_model = item.getModel();
>                Customer customer = customer_model.getObject();
>                item.add(new Label("id", String.valueOf(customer.getId())));
>                Link link = new Link("link")
>                    {
>                    @Override
>                    public void onClick()
>                        {
>                        Debug.log.out("CustomerListPage.onClick() - customer: " +
> customer_model.getObject().getName());
>                        setResponsePage(new CustomerPage(customer_model.getObject()));
>                        }
>                    };
>                item.add(link);
>                link.add(new Label("name", customer.getName()));
>                String date_string = "";
>                Date date = customer.getLastActivity();
>                if (date != null)                         // TODO should this really ever be null?
>                    date_string = DateUtil.toSimpleDateString(date);
>                item.add(new Label("date", date_string));
>                }
>            };
>
> So I'm obviously missing something important about the right way to do this.
>
> Can someone point me in the right direction?
>
> TIA!
> Chris
>
>
>
> --
> ------------------------------------------------------------------------ -
> Chris Merrill                           |  Web Performance, Inc.
> chris@webperformance.com                |  http://webperformance.com
> 919-433-1762                            |  919-845-7601
>
> Web Performance: Website Load Testing Software & Services
> ------------------------------------------------------------------------ -
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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