且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是Wicket中的ResourceReferences,它们如何工作?

更新时间:2023-11-23 08:17:16

要扩展Andrew的答案:

To expand on Andrew's answer:

A ResourceReference 本身只是一个引用到可通过 SharedResources 获得的资源。您添加到 SharedResources 的任何资源(通常在您的 Application#init中完成( ))具有您定义的名称。然后,任何使用资源的组件都可以通过带有该名称的 ResourceReference 引用此共享资源 - 因此参数为叫名字。在这种情况下,不需要scope参数(类)。

A ResourceReference per se is nothing but a reference to a resource available through SharedResources. Any kind of Resource that you add to SharedResources (usually done in your Application#init()) has a name that you define. Any Component that uses a resource can then refer to this shared resource through a ResourceReference with that name - hence the parameter being called "name". In this case the scope parameter (the class) is not needed.

这是一般情况,用于引用任何类资源。

This is the general case, to refer to any kind of Resource.

您和Andrew的示例中显示的案例更为特殊:您的 ResourceReference name 不引用先前添加到 SharedResources 资源。这里一个所谓的 PackageResource 被懒惰地初始化并添加到 SharedResources

The case shown in your and Andrew's examples is a more special case: Your ResourceReference's name does not refer to a Resource previously added to SharedResources. Here a so-called PackageResource is lazily initialized and added to SharedResources.

PackageResource 实际上是整个从文件加载文件的东西。

PackageResource is what actually does the whole "load-file-from-classpath" stuff.

因此,如果您只想从类路径中引用类似图像的文件,安德鲁的示例只是一个非常有用的快捷方式,可以避免自己创建 PackageResource 。如上所述, ResourceReference 还有更多: - )

So if you want to just refer to a file like an image from your classpath, Andrew's example is simply a very useful shortcut to avoid creating that PackageResource yourself. As noted above, there is more to ResourceReference than just that :-)