如何引用第三方库源代码以供客户端在 GWT 中使用?
GWT 文档规定,在客户端编译为 JavaScript 的所有源代码都必须位于 gwt.xml
文件的子包中。当引用第三方库时,这是如何工作的?
具体来说,如果我有一个库 foo.jar
并且我想使用一些 POJO(可序列化的)并且不吸收任何非可序列化的 code> code,我该如何使用这些 POJO?我如何告诉 GWT 源代码在哪里?
The GWT documentation states that all the source code for compilation to JavaScript on the client-side must be in a subpackage of the gwt.xml
file. How does this work for when one references third party libraries?
Specifically, if I have a library foo.jar
and I want to use some POJOs (which are Serializable
) and do not suck in any non-Serializable
code, how can I use these POJOs? How do I tell GWT where the source code is for them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还要记住,GWT 编译器需要实际的 Java 源代码才能编译为 javascript,因此类可用并且所有类都可序列化是不够的。对于通过网络发送类的 RPC,它们必须能够在到达客户端时用作 JS。
也就是说,请查看 gwt-user 中的模块
javax/validation/Validation.gwt.xml
。将此文件放在那里,以便 javax.validation 中的其他包(甚至其他 jar 中的包)可以编译成 JS 以进行客户端验证。如果您的类路径上有一个 jar(和源代码),其代码位于com.thirdparty.pojos
中,您可以在您自己的项目中在您自己的源代码的同一目录中创建一个模块文件(类似于 < code>com/thirdparty/pojos/ThirdParthRpc.gwt.xml,并在其中放入
元素以表明整个包对于翻译为JS。可能会有一些文件无法翻译 - 使用排除标签来处理这些文件。Remember too that the GWT compiler needs actual Java source to compile to javascript, so it isn't enough that the classes are available and that all are serializable. For RPC to send the classes over the wire, they must be able to be used as JS when they get to the client.
That said, take a look in gwt-user, at the module
javax/validation/Validation.gwt.xml
. This file is put there so that other packages injavax.validation
- even in other jars - can be compiled into JS for client-side validation. if you have a jar (and sources) on your classpath with code incom.thirdparty.pojos
, you can create a module file in your own project in that same dir in your own source (something likecom/thirdparty/pojos/ThirdParthRpc.gwt.xml
, and put a<source path="" />
element in it to indicate that the entire package is legal for translation to JS. There will likely be some files that cannot be translated - use the exclude tag to deal with those.如果您可以控制将 foo 打包到 foo.jar 中,并且拥有所有源代码,那么这很容易。
如果你有一个打包好的 foo.jar,并且恰好有源代码,那么你需要展开 foo.jar,将源代码复制到展开的 .jar 目录中,生成一个简单的 GWT module.xml 文件并添加一个标签你的项目的模块。
If you have control over packaging foo into foo.jar, and you have all the sources, then it's easy.
If you have a packaged foo.jar, and happen to have the source code, then you need to expand the foo.jar, copy the source into the exploded .jar directory, generate a simple GWT module.xml file and add an tag to your project’s module.