You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Felipe Issa <fe...@simova.com.br> on 2011/11/21 18:04:31 UTC

getText returns NULL when formating number using l10n

Hi. I'm trying to internationalize my application, and for that i need 
to use the struts l10n. I have managed to parse the number according to 
the user locale, but I'm having some troubles to display the number 
using the user locale.

On Struts 2 documentation it says:

"to input a number, one might have noticed that the number is always 
shown in the Java default number format. Not only that this is not 
"nice", if you are in a non-en locale, it will also cause trouble when 
submitting the form since type conversion is locale aware. The solution 
is to again use the message formats as defined above, by using the 
getText Method of ActionSupport:

<s:textfield key="orderItem.price" 
value="%{getText('format.number',{orderItem.price})}" />

This maps to the method signature getText( String key, Object[] params ) 
in ActionSupport."

(http://struts.apache.org/2.x/docs/formatting-dates-and-numbers.html)

It display the number in the correct format when i use the value like 
this, but when the value is null it shows "null" instead of a empty 
textfield. Does anyone know how to fix it?

Thanks in advice!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: getText returns NULL when formating number using l10n

Posted by Li Ying <li...@gmail.com>.
I have read the source of java.text.MessageFormat, looks like it
output a "null" text to the result String when the argument is null.

I think you can create a read-only property in your action (or your
data model class), and implement the null check and format process in
this property,
code like:
public String getPriceDisplay(String formatPattern) {
      if (this.price == null) {
         return ""
     }
     else {
         format it by the formatPattern
      }
}

and use this property in the jsp tag,
code like:

<s:textfield key="orderItem.price"
value="%{orderItem.getPriceDisplay(getText('format.number'))}" />


2011/11/22 Felipe Issa <fe...@simova.com.br>:
> Hi. I'm trying to internationalize my application, and for that i need to
> use the struts l10n. I have managed to parse the number according to the
> user locale, but I'm having some troubles to display the number using the
> user locale.
>
> On Struts 2 documentation it says:
>
> "to input a number, one might have noticed that the number is always shown
> in the Java default number format. Not only that this is not "nice", if you
> are in a non-en locale, it will also cause trouble when submitting the form
> since type conversion is locale aware. The solution is to again use the
> message formats as defined above, by using the getText Method of
> ActionSupport:
>
> <s:textfield key="orderItem.price"
> value="%{getText('format.number',{orderItem.price})}" />
>
> This maps to the method signature getText( String key, Object[] params ) in
> ActionSupport."
>
> (http://struts.apache.org/2.x/docs/formatting-dates-and-numbers.html)
>
> It display the number in the correct format when i use the value like this,
> but when the value is null it shows "null" instead of a empty textfield.
> Does anyone know how to fix it?
>
> Thanks in advice!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: getText returns NULL when formating number using l10n

Posted by Li Ying <li...@gmail.com>.
If you want a definitive solution, i suggest you create a new method like:
    getTextEx(.....)
in your action,
in this method, you can check the parameters, if they are null, change
them to empty string, and then invoke the original getText method.

you can put this method in the base class of your actions, so you can
use it in all the actions.

2011/11/23 Felipe Issa <fe...@simova.com.br>:
> It's not a definitive solution, but it works. Thanks Li Ying!
>
> Em 21/11/2011 23:07, Li Ying escreveu:
>>
>> or you can do the null check in the jsp code, before you create the
>> param list for the format string.
>>
>> Code like:
>>
>> <s:textfield
>>     key="orderItem.price"
>>     value="%{getText('format.number',{orderItem.price == null ? "" :
>> orderItem.price})}" />
>>
>>
>>
>> 2011/11/22 Felipe Issa<fe...@simova.com.br>:
>>>
>>> Hi. I'm trying to internationalize my application, and for that i need to
>>> use the struts l10n. I have managed to parse the number according to the
>>> user locale, but I'm having some troubles to display the number using the
>>> user locale.
>>>
>>> On Struts 2 documentation it says:
>>>
>>> "to input a number, one might have noticed that the number is always
>>> shown
>>> in the Java default number format. Not only that this is not "nice", if
>>> you
>>> are in a non-en locale, it will also cause trouble when submitting the
>>> form
>>> since type conversion is locale aware. The solution is to again use the
>>> message formats as defined above, by using the getText Method of
>>> ActionSupport:
>>>
>>> <s:textfield key="orderItem.price"
>>> value="%{getText('format.number',{orderItem.price})}" />
>>>
>>> This maps to the method signature getText( String key, Object[] params )
>>> in
>>> ActionSupport."
>>>
>>> (http://struts.apache.org/2.x/docs/formatting-dates-and-numbers.html)
>>>
>>> It display the number in the correct format when i use the value like
>>> this,
>>> but when the value is null it shows "null" instead of a empty textfield.
>>> Does anyone know how to fix it?
>>>
>>> Thanks in advice!
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: getText returns NULL when formating number using l10n

Posted by Felipe Issa <fe...@simova.com.br>.
It's not a definitive solution, but it works. Thanks Li Ying!

Em 21/11/2011 23:07, Li Ying escreveu:
> or you can do the null check in the jsp code, before you create the
> param list for the format string.
>
> Code like:
>
> <s:textfield
>      key="orderItem.price"
>      value="%{getText('format.number',{orderItem.price == null ? "" :
> orderItem.price})}" />
>
>
>
> 2011/11/22 Felipe Issa<fe...@simova.com.br>:
>> Hi. I'm trying to internationalize my application, and for that i need to
>> use the struts l10n. I have managed to parse the number according to the
>> user locale, but I'm having some troubles to display the number using the
>> user locale.
>>
>> On Struts 2 documentation it says:
>>
>> "to input a number, one might have noticed that the number is always shown
>> in the Java default number format. Not only that this is not "nice", if you
>> are in a non-en locale, it will also cause trouble when submitting the form
>> since type conversion is locale aware. The solution is to again use the
>> message formats as defined above, by using the getText Method of
>> ActionSupport:
>>
>> <s:textfield key="orderItem.price"
>> value="%{getText('format.number',{orderItem.price})}" />
>>
>> This maps to the method signature getText( String key, Object[] params ) in
>> ActionSupport."
>>
>> (http://struts.apache.org/2.x/docs/formatting-dates-and-numbers.html)
>>
>> It display the number in the correct format when i use the value like this,
>> but when the value is null it shows "null" instead of a empty textfield.
>> Does anyone know how to fix it?
>>
>> Thanks in advice!
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: getText returns NULL when formating number using l10n

Posted by Li Ying <li...@gmail.com>.
or you can do the null check in the jsp code, before you create the
param list for the format string.

Code like:

<s:textfield
    key="orderItem.price"
    value="%{getText('format.number',{orderItem.price == null ? "" :
orderItem.price})}" />



2011/11/22 Felipe Issa <fe...@simova.com.br>:
> Hi. I'm trying to internationalize my application, and for that i need to
> use the struts l10n. I have managed to parse the number according to the
> user locale, but I'm having some troubles to display the number using the
> user locale.
>
> On Struts 2 documentation it says:
>
> "to input a number, one might have noticed that the number is always shown
> in the Java default number format. Not only that this is not "nice", if you
> are in a non-en locale, it will also cause trouble when submitting the form
> since type conversion is locale aware. The solution is to again use the
> message formats as defined above, by using the getText Method of
> ActionSupport:
>
> <s:textfield key="orderItem.price"
> value="%{getText('format.number',{orderItem.price})}" />
>
> This maps to the method signature getText( String key, Object[] params ) in
> ActionSupport."
>
> (http://struts.apache.org/2.x/docs/formatting-dates-and-numbers.html)
>
> It display the number in the correct format when i use the value like this,
> but when the value is null it shows "null" instead of a empty textfield.
> Does anyone know how to fix it?
>
> Thanks in advice!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org