同一类的 GWT 客户端和服务器实现
有没有办法让同一个类在客户端和服务器上以不同的方式实现?
避免“你为什么要这样做?”问题..我将详细说明
我正在转换一个非常大的 Java 客户端/服务器应用程序。目前它使用 Swing GUI 客户端并通过 Spring 远程处理 (RPC) 与服务器通信。将 GWT RPC 与 Spring 服务结合使用不是问题,有几个优秀的示例可用,而且似乎都运行良好。
客户端和服务器共有的几个类包含来回传递的数据。这些类还包含一些使用标准 JRE 类实现的行为。例如,一个类以区域设置特定的方式包含、解析和格式化日期和时间,包括时区、DST 等。我可以重写/重构它,但应用程序有超过 1000 万个 SLOC,导致仅对此类就有数百万个引用,因此重大重写并不具有成本效益。
以此为例,GWT 为解析和格式化日期提供了出色的 i18n 支持。但实现方式与 JRE 的方式不同。
因此,我正在寻找一种更有效的方法,可以将实现注入到 DateTime 类的 shell 中,具体取决于它是在客户端(使用 GWT 和本机 JS)还是在服务器中(使用 JRE)。有什么巧妙的方法可以做到这一点吗?也许使用模块文件 XXXXX.gwt.xml。我正在寻找通用的解决方案。
Is there any way to have the same class implemented differently on the client vs the server?
To avoid the "Why do you want to do that?" question.. I will elaborate
I am converting a very large Java client/server application. Currently it uses a Swing GUI client and talks to the server via Spring remoting (RPC). Using GWT RPC with Spring services is not a problem, there are several excellent examples available and the all seem to work well.
Several classes that are common to both the client and the server contain data that is passed back and forth. These classes also contain some behavior that is implemented by using the standard JRE classes. For example, one class contains, parses and formats date and time, including time zone, DST, etc. in a locale specific way. I could rewrite/refactor it but the application is over 10 million SLOC, resulting in literally millions of references to this class alone, so a major rewrite is not cost effective.
To use this as an example, GWT provides excellent i18n support for parsing and formatting dates. But the implementation is different to the way the JRE does it.
So I'm looking for a cleaver way where by I can inject an implementation into the shell of my DateTime class, depending on whether it is in the client (using GWT and native JS) or in the server (using the JRE). Is there a cunning way to do this? Perhaps using the module file XXXXX.gwt.xml. I'm looking for a generic solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用< /code>
用于用另一个包实现覆盖。
GWT 使用它来模拟 Java 运行时类,并且(除其他外)为
com.google.gwt.regexp.shared.*
类的客户端和服务器提供不同的实现。You'd want to use the
<super-source>
for overriding one package implementation with another.This is what GWT uses to emulate the Java Runtime classes, and (among others) provide different implementations for client and server of the
com.google.gwt.regexp.shared.*
classes.我认为您正在寻找的是:项目 gwt.xml 文件中的
。它告诉 GWT 生成器在哪里寻找客户端代码以转换为 JS。在我的项目中,我是这样设置的:
基本上客户端代码位于客户端目录中,并且在共享中我们为客户端和服务器端保留bean和一些数据包装器。
您可以做的就是使用上面的源路径将要转换的包添加到客户端。但您必须记住,您要转换的类只能由 GWT 生成器可以转换为客户端 java 脚本的对象和属性组成。我也不确定是否可以在源路径中放入更准确的路径,例如:
另一个缺点是,如果您使用 GWT i18n 支持,它会在编译时自行处理不同的语言环境 - 这很好。如果您决定使用自己的机制,您的类必须包含一些逻辑来了解当前使用的语言环境,这些逻辑必须与 GWT 兼容。
I think what You are looking for, is this:
<source path="client" />
in your project gwt.xml file. It tells the GWT generator where to look for client side code to convert to JS. In my project I have it set up this way:Basically client code is in the client directory, and in shared we keep beans and some data wrappers for client and server side.
What you could do, is to add the packages you want to convert to client with the source path like above. But you must remember, that the classes you are going to convert, can be composed only of objects and properties which GWT generator can convert into client-side java script. I'm not sure also if more accurate path can be put in the source path, like:
Another drawback is, that if you use GWT i18n support, it handles different locale in time of compilation by its own - which is good. If you decide to use your own mechanism, your classes must contain some logic to be aware of locale being used currently, which must be compatible with GWT.