在共享包中使用GWT的NumberFormat类 [英] Using GWT's NumberFormat class in the shared package

查看:126
本文介绍了在共享包中使用GWT的NumberFormat类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GWT项目中,我的服务返回了我定义的 Shield 类型的对象。由于客户端和服务器都使用了 Shield 类型,因此我已将类定义放入共享包中。



> Shield 类使用 com.google.gwt.i18n.client.NumberFormat 类(替代了其中的 java.text.DecimalFormat )。



问题是无法将 NumberFormat 放入共享包中,因为它会创建 LocaleInfo 的实例使用GWT.create()。



有没有什么办法可以使用 com.google.gwt.i18n.client.NumberFormat 共享软件包?

解决方案

简单地说,不需要共享软件包 p

应该只包含客户端和服务器都使用(AND CAN)的任何逻辑或数据类型。



gwt提供数字格式类的原因是, w ords -


在某些类中,类的功能太昂贵而无法完全仿真,所以类似的例程提供另一个包。

反之亦然, NumberFormat 的GWT实现是javascript特定的,当然不能用在服务器端(在你的情况下是Java)。

您必须尝试将格式化逻辑从这个类中移出,并分别移入服务器端(使用java的NumberFormat)和客户端(使用gwt的NumberFormat)。您可以将其余部分保留在共享包中。


In my GWT project my service returns an object of type Shield that I have defined. As the Shield type is being used by both the client and server, I have put the class definition in the shared package.

The Shield class uses the com.google.gwt.i18n.client.NumberFormat class (a replacement for, amongst others, java.text.DecimalFormat).

The issue is that NumberFormat can't be put in the shared package as it creates an instance of LocaleInfo using GWT.create().

Is there any way I can use com.google.gwt.i18n.client.NumberFormat from within the shared package?

解决方案

In short, No.

Shared packages should only contain any logic or data types that are (AND CAN) used by both the client and the server.

The reason gwt provides their number format class is, in their words -

In some classes, the functionality of the class is too expensive to be emulated entirely, so a similar routine in another package is provided instead.

Vice versa, GWTs implementation of NumberFormat is javascript specific and of course cannot be used in the server side (Java in your case).

You will have to try and move the formatting logic out of this class and into server side (using java's NumberFormat) and client side (using gwt's NumberFormat) respectively. You can keep the rest of it in the shared package.

这篇关于在共享包中使用GWT的NumberFormat类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆