You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Guillaume Simard <gu...@gmail.com> on 2009/06/09 19:42:28 UTC

PropertyModel property expression fallback

Hi everyone !
I have two questions.

First, I have a PropertyModel ..

Something similar to this :

add ( new Label("firstResidentName", new PropertyModel<House>(myHouse,
"residents[0].name") ) );

Where House.getResidents() returns a Collection<Person>
and Person.getName() returns the Person's name (a String).

Now say I use this PropertyModel on a House with no residents. I'll get
a IndexOutOfBoundsException.
Is there a way to define a default value for the Label in case the
PropertyModel cannot reach the property ?

In my current situation, I cannot verify directly (before defining the
PropertyModel) if "myHouse" has any residents so the solution would have to
be related to the Label or the Model.

Second,

Using the same example, should my PropertyModel be " PropertyModel<House> "
or " PropertyModel<String> " ? Person.getName() returns a String.

Thanks in advance !

Sincerely,
Guillaume

Re: PropertyModel property expression fallback

Posted by Guillaume Simard <gu...@gmail.com>.
Thanks a lot guys !
This seems to be a question you get often, sadly I really tried googling an
answer before posting here..
But I didn't find anything :(

Thanks !
Guillaume

On Tue, Jun 9, 2009 at 2:02 PM, Martijn Dashorst <martijn.dashorst@gmail.com
> wrote:

> Just create a method House#getFirstResident() that return null if there's
> none.
>
> And the generic type of the model should be the type that is the
> result of getObject().
>
> Martijn
>
> On Tue, Jun 9, 2009 at 7:42 PM, Guillaume Simard<gu...@gmail.com> wrote:
> > Hi everyone !
> > I have two questions.
> >
> > First, I have a PropertyModel ..
> >
> > Something similar to this :
> >
> > add ( new Label("firstResidentName", new PropertyModel<House>(myHouse,
> > "residents[0].name") ) );
> >
> > Where House.getResidents() returns a Collection<Person>
> > and Person.getName() returns the Person's name (a String).
> >
> > Now say I use this PropertyModel on a House with no residents. I'll get
> > a IndexOutOfBoundsException.
> > Is there a way to define a default value for the Label in case the
> > PropertyModel cannot reach the property ?
> >
> > In my current situation, I cannot verify directly (before defining the
> > PropertyModel) if "myHouse" has any residents so the solution would have
> to
> > be related to the Label or the Model.
> >
> > Second,
> >
> > Using the same example, should my PropertyModel be " PropertyModel<House>
> "
> > or " PropertyModel<String> " ? Person.getName() returns a String.
> >
> > Thanks in advance !
> >
> > Sincerely,
> > Guillaume
> >
>
>
>
> --
> Become a Wicket expert, learn from the best: http://wicketinaction.com
> Apache Wicket 1.3.5 is released
> Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: PropertyModel property expression fallback

Posted by Martijn Dashorst <ma...@gmail.com>.
Just create a method House#getFirstResident() that return null if there's none.

And the generic type of the model should be the type that is the
result of getObject().

Martijn

On Tue, Jun 9, 2009 at 7:42 PM, Guillaume Simard<gu...@gmail.com> wrote:
> Hi everyone !
> I have two questions.
>
> First, I have a PropertyModel ..
>
> Something similar to this :
>
> add ( new Label("firstResidentName", new PropertyModel<House>(myHouse,
> "residents[0].name") ) );
>
> Where House.getResidents() returns a Collection<Person>
> and Person.getName() returns the Person's name (a String).
>
> Now say I use this PropertyModel on a House with no residents. I'll get
> a IndexOutOfBoundsException.
> Is there a way to define a default value for the Label in case the
> PropertyModel cannot reach the property ?
>
> In my current situation, I cannot verify directly (before defining the
> PropertyModel) if "myHouse" has any residents so the solution would have to
> be related to the Label or the Model.
>
> Second,
>
> Using the same example, should my PropertyModel be " PropertyModel<House> "
> or " PropertyModel<String> " ? Person.getName() returns a String.
>
> Thanks in advance !
>
> Sincerely,
> Guillaume
>



-- 
Become a Wicket expert, learn from the best: http://wicketinaction.com
Apache Wicket 1.3.5 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.

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


Re: PropertyModel property expression fallback

Posted by Jeremy Thomerson <je...@wickettraining.com>.
You could see my blog post [1] and adapt the DefaultIfNullModel to fit
your needs.  Then just wrap your property model in your newly created
model.  Or extend PropertyModel, override getObject and implement this
in your own custom property model.  I prefer composition over
inheritance because you can reuse it more places - but it's your call.

[1] - http://www.jeremythomerson.com/blog/2008/11/06/wicket-the-power-of-nested-models/

--
Jeremy Thomerson
http://www.wickettraining.com




On Tue, Jun 9, 2009 at 12:42 PM, Guillaume Simard<gu...@gmail.com> wrote:
> Hi everyone !
> I have two questions.
>
> First, I have a PropertyModel ..
>
> Something similar to this :
>
> add ( new Label("firstResidentName", new PropertyModel<House>(myHouse,
> "residents[0].name") ) );
>
> Where House.getResidents() returns a Collection<Person>
> and Person.getName() returns the Person's name (a String).
>
> Now say I use this PropertyModel on a House with no residents. I'll get
> a IndexOutOfBoundsException.
> Is there a way to define a default value for the Label in case the
> PropertyModel cannot reach the property ?
>
> In my current situation, I cannot verify directly (before defining the
> PropertyModel) if "myHouse" has any residents so the solution would have to
> be related to the Label or the Model.
>
> Second,
>
> Using the same example, should my PropertyModel be " PropertyModel<House> "
> or " PropertyModel<String> " ? Person.getName() returns a String.
>
> Thanks in advance !
>
> Sincerely,
> Guillaume
>

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