You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by sorinev <so...@gmail.com> on 2017/12/01 17:54:23 UTC

Force Model to use getter

Wicket 7.9.0.

I have a Java double that is linked to a Wicket Label. This double
represents a dollar amount, so I want to work with it as a double for math
purposes. When I display it on the page however, I want it to a be a
currency formatted String.

I figured I would just add a getter to my page for that property, with a
return type of String (and do the appropriate actions there). So for a
member variable called 'amount' of type Double, I have a getter as 'private
String getAmount()'. It's not working though, it still gets the value
directly from the property itself when displaying on the page (and seems to
get displayed as an int, at that).

I was using CompoundPropertyModel at first, then after something i read on 
StackOverflow
<https://stackoverflow.com/questions/548354/wicket-propertymodel-strangeness> 
, I changed it to a PropertyModel, but it's still not working. Does the
return type of the getter have to match the type of the variable, even if
the getter method has the appropriate name otherwise? I am currently going
with the method shown in the second answer by Andreas on that StackOverflow
question, but I'd rather it be done automatically through PropertyModel or
CompoundPropertyModel because it looks a lot cleaner.

--
Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html

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


Re: Force Model to use getter

Posted by Andrew Geery <an...@gmail.com>.
How about using an IConverter since the issue is not about the data itself
but the representation of it?

For example, you could create a Label class that overrides the
getConverter() and return a BigDecimalConverter object that overrides
the getNumberFormat method to return something like
DecimalFormat.getCurrencyInstance().

Andrew


On Fri, Dec 1, 2017 at 3:39 PM, sorinev <so...@gmail.com> wrote:

> I added a setter for each property, but it's still not working (tried
> matching field types as well). I guess I'll just have to stick with the
> IModel route, even though I don't like it.
>
> I changed the Doubles to BigDecimal, thanks for the reminder.
>
> --
> Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-
> f1842947.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Force Model to use getter

Posted by sorinev <so...@gmail.com>.
I added a setter for each property, but it's still not working (tried
matching field types as well). I guess I'll just have to stick with the
IModel route, even though I don't like it.

I changed the Doubles to BigDecimal, thanks for the reminder.

--
Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html

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


Re: Force Model to use getter

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

The "problem" you face is that Wicket tries to find both getter and setter
[1] and if any of them do not exist then it falls back to the field.

I'd continue using a custom IModel for this use case.

And I'd also change the type of the "amount" to BigDecimal! Doing math with
doubles when dealing with money is a bomb with a timer!


1.
https://github.com/apache/wicket/blob/51d9e533dc3a45183210a1f4852851b485f02ea3/wicket-core/src/main/java/org/apache/wicket/core/util/lang/PropertyResolver.java#L274

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Fri, Dec 1, 2017 at 7:54 PM, sorinev <so...@gmail.com> wrote:

> Wicket 7.9.0.
>
> I have a Java double that is linked to a Wicket Label. This double
> represents a dollar amount, so I want to work with it as a double for math
> purposes. When I display it on the page however, I want it to a be a
> currency formatted String.
>
> I figured I would just add a getter to my page for that property, with a
> return type of String (and do the appropriate actions there). So for a
> member variable called 'amount' of type Double, I have a getter as 'private
> String getAmount()'. It's not working though, it still gets the value
> directly from the property itself when displaying on the page (and seems to
> get displayed as an int, at that).
>
> I was using CompoundPropertyModel at first, then after something i read on
> StackOverflow
> <https://stackoverflow.com/questions/548354/wicket-
> propertymodel-strangeness>
> , I changed it to a PropertyModel, but it's still not working. Does the
> return type of the getter have to match the type of the variable, even if
> the getter method has the appropriate name otherwise? I am currently going
> with the method shown in the second answer by Andreas on that StackOverflow
> question, but I'd rather it be done automatically through PropertyModel or
> CompoundPropertyModel because it looks a lot cleaner.
>
> --
> Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-
> f1842947.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>