在Struts2.0.11.1版本,相应的指南中
[url]http://struts.apache.org/2.0.11.1/docs/formatting-dates-and-numbers.html[/url]
Localizing form data with getText
Localizing form data with getText
[b]Placing a textfield in a form like this[/b]
[quote]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:[/quote]
[quote]This maps to the method signature getText( String key, Object[] params ) in ActionSupport.[/quote]
我在页面中如下使用:
发现调用的是:
getText( String key, List args) in ActionSupport.
而非
getText( String key, Object[] params ) in ActionSupport
当然,这二个方法其实都调用同一个函:
详见:xwork-2.04,TextProviderSupport.java
最终发现,argsArrays为:['orderOprRecord.jsFee']
最后在LocalizedTextUtil.java,第590行
String msg = mf.format(args);
这时,agrs内的值仍是:['orderOprRecord.jsFee']
自始自终没有经过处理,即没有在ValueStack中取出对应的值.
所以msg最后的值为空.
下面的难道是:struts2的手册的作者的笔误吗?
[url]http://struts.apache.org/2.0.11.1/docs/formatting-dates-and-numbers.html[/url]
Localizing form data with getText
Localizing form data with getText
[b]Placing a textfield in a form like this[/b]
<s:textfield key="orderItem.price" />[quote]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:[/quote]
<s:textfield key="orderItem.price" value="%{getText('format.number',{'orderItem.price'})}" />[quote]This maps to the method signature getText( String key, Object[] params ) in ActionSupport.[/quote]
我在页面中如下使用:
<s:textfield key="orderOprRecord.jsFee" value="%{getText('format.number',{'orderOprRecord.jsFee'})}" size="8"/>发现调用的是:
getText( String key, List args) in ActionSupport.
而非
getText( String key, Object[] params ) in ActionSupport
当然,这二个方法其实都调用同一个函:
LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);详见:xwork-2.04,TextProviderSupport.java
最终发现,argsArrays为:['orderOprRecord.jsFee']
最后在LocalizedTextUtil.java,第590行
if (message != null) {
MessageFormat mf = buildMessageFormat(TextParseUtil.translateVariables(message, valueStack), locale);
String msg = mf.format(args);
result = new GetDefaultMessageReturnArg(msg, found);
}String msg = mf.format(args);
这时,agrs内的值仍是:['orderOprRecord.jsFee']
自始自终没有经过处理,即没有在ValueStack中取出对应的值.
所以msg最后的值为空.
下面的难道是:struts2的手册的作者的笔误吗?
<s:textfield key="orderItem.price" value="%{getText('format.number',{'orderItem.price'})}" />
本文探讨了Struts2.0.11.1版本中数值格式化的问题,特别是如何通过getText方法正确地设置数值格式以适应不同的区域设置。文章详细分析了一个具体案例,指出手册中的潜在错误。

被折叠的 条评论
为什么被折叠?



