论坛首页 Java企业应用论坛

Struts2 文本域中数值格化的疑问

浏览 2810 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-12  
在Struts2.0.11.1版本,相应的指南中
http://struts.apache.org/2.0.11.1/docs/formatting-dates-and-numbers.html
Localizing form data with getText
Localizing form data with getText
Placing a textfield in a form like this

<s:textfield key="orderItem.price" />

引用
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.


我在页面中如下使用:
<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'})}" />
   发表时间:2008-04-13  

所以现在我还是推荐用自己写的NUMBER插件。

 struts2-number-plugin插件简单源代码

0 请登录后投票
   发表时间:2008-04-24  
<s:textfield key="orderItem.price" value="%{getText('format.number',{orderItem.price})}" />

单引号去掉才对.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics