java 如何在 GWT 中使用 ClientBundle?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3447311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ClientBundle in GWT?
提问by azec-pdx
I am using GWT ClientBundle for the first time. I wrote interface that extends it and here is the code:
我第一次使用 GWT ClientBundle。我编写了扩展它的接口,这是代码:
package edu.etf.fuzzy.client.login;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
public interface Resources extends ClientBundle {
@Source("logo_federacija.jpg")
ImageResource logo();
@Source("shim.gif")
ImageResource shim();
}
My question is: How do I tell where exactly these resources (images in this case) can be found. Should I put them: a) in the same directory as interface - (ugly)? b) in some specific directory that never can be changed ? c) in directory that I want, but there should be mechanism to specify it - (would be great)?
我的问题是:我如何知道在哪里可以找到这些资源(在这种情况下是图像)。我应该将它们:a) 与界面放在同一目录中 -(丑陋)?b) 在某些永远无法更改的特定目录中?c)在我想要的目录中,但应该有机制来指定它 - (会很棒)?
Thank you.
谢谢你。
回答by Igor Klimer
I usually create a new package for resources (com.example.project.resource) and put the ClientBundleinterface and the appropriate resources there (together, as in in one directory) - it's one java file plus a bunch of images, no need for further separation, IMHO.
我通常为资源 ( com.example.project.resource)创建一个新包,并将 ClientBundle界面和适当的资源放在那里(一起放在一个目录中) - 它是一个 java 文件加上一堆图像,无需进一步分离,恕我直言。
As for the paths to the resources - you can pass them via @Source("path")annotation. For example, if you have your ClientBundleinterface in com.example.project.resourceand images in com.example.project.images, then you'd write @Source("../images/shim.gif"). You could probably use absolute paths, but make sure you store them in a static variable and share amongst the resource, so it can be easily overridden ;)
至于资源的路径 - 您可以通过@Source("path")注释传递它们。例如,如果您的ClientBundle界面在 中com.example.project.resource,图像在 中com.example.project.images,那么您将编写@Source("../images/shim.gif"). 您可能可以使用绝对路径,但请确保将它们存储在一个静态变量中并在资源之间共享,因此它可以很容易地被覆盖;)
回答by Joseph Lust
Similar to Igor's points:
类似于 Igor 的观点:
com.yoursite.resources: YourClientBundle.java
com.yoursite.resources.css: YourCssResource.java
com.yoursite.resources.images: image files here
The key here is that if you don't consume a resource (CSS/Image/Text), the GWT compiler will ignore it. This prevents war bloatas your application gets holder and you've got hundreds of images and are not sure which are used and which are not.
这里的关键是,如果您不使用资源(CSS/图像/文本),GWT 编译器将忽略它。这可以防止War膨胀,因为您的应用程序获得了持有者,并且您有数百张图像并且不确定哪些被使用,哪些没有。
If you use the option of dropping everything in the public/imagesfolder, then all of the items will be included in your application war, regardless of whether they're used.
如果您使用将所有内容放入public/images文件夹中的选项,那么所有项目都将包含在您的应用程序War中,无论它们是否被使用。

