浏览 2810 次
锁定老帖子 主题:Struts2 文本域中数值格化的疑问
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-12
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'})}" /> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-04-13
所以现在我还是推荐用自己写的NUMBER插件。 |
|
返回顶楼 | |
发表时间:2008-04-24
<s:textfield key="orderItem.price" value="%{getText('format.number',{orderItem.price})}" />
单引号去掉才对. |
|
返回顶楼 | |