在我的 GWT 项目中,我的服务返回一个类型的对象Shield我已经定义了。作为Shield客户端和服务器都使用类型,我已将类定义放在共享包中。
The Shield类使用com.google.gwt.i18n.client.NumberFormat类(替代,除其他外,java.text.DecimalFormat).
问题是数字格式不能放入共享包中,因为它创建了一个实例语言环境信息使用 GWT.create()。
有什么办法我可以使用com.google.gwt.i18n.client.NumberFormat从共享包中?
我通过创建 SharedNumberFormat 然后为从未使用过的服务器版本创建一个空的客户端存根解决了这个问题。
这是我的 SharedNumberFormat.java,您猜对了,它可以在共享代码中使用,并且在客户端和服务器端都可以正常工作:
import java.text.DecimalFormat;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
/**
* The purpose of this class is to allow number formatting on both the client and server side.
*/
public class SharedNumberFormat
{
private String pattern;
public SharedNumberFormat(String pattern)
{
this.pattern = pattern;
}
public String format(Number number)
{
if(GWT.isClient())
{
return NumberFormat.getFormat(pattern).format(number);
} else {
return new DecimalFormat(pattern).format(number.doubleValue());
}
}
}
然后我在我的超级源中删除了 java.text.DecimalFormat 实现:
package java.text;
/**
* The purpose of this class is to allow Decimal format to exist in Shared code, even though it is never called.
*/
@SuppressWarnings("UnusedParameters")
public class DecimalFormat
{
public DecimalFormat(String pattern) {}
public static DecimalFormat getInstance() {return null;}
public static DecimalFormat getIntegerInstance() {return null;}
public String format(double num) {return null;}
public Number parse(String num) {return null;}
}
我在那里有额外的方法,因为我使用该类服务器端,如果它们不存在,编译器就会陷入困境。
最后,不要忘记将 super-source 标签添加到 *.gwt.xml 中:
<super-source path="clientStubs"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)