什么是Wicket中的ResourceReferences,它们如何工作? [英] What are ResourceReferences in Wicket, and how do they work?
问题描述
我见过包含以下内容的示例:
I have seen examples containing things like this:
mountSharedResource("/images/logo.gif", new ResourceReference(ImageScope.class,
"logo.gif").getSharedResourceKey());
mountSharedResource("/resource",
Application.class.getName() + "/" + resourceKey);
但是Javadoc为构造函数说明了这一点:
But the Javadoc says this for the constructor:
ResourceReference(java.lang.Class<?> scope, java.lang.String name);
所以当你创建一个 ResourceReference
时,你给它上课。为什么?通常你会想要全局范围或刚刚创建的 ResourceReference
对象的范围,不是吗?
So when you create a ResourceReference
, you give it a class. Why? Usually you would want either global scope or the scope of the ResourceReference
object you have just created, no?
什么是名称
?它是 sharedResourceKey
吗?如果没有,那么 resourceKey
来自哪里?它是如何产生的,为什么不是它的名字?或者是 name
在类路径上查找并神奇地加载(假设在类路径中只有一个具有该名称的文件;如果有多个会发生什么?)?如果它确实加载了具有该名称的文件,为什么不在Javadoc中这样说呢?
Also, what is name
? Is it the sharedResourceKey
? If not, where does the resourceKey
come from? How is it generated, and why isn't it the name? Or is name
looked up on the classpath, and magically loaded (assuming that there is only one file with that name on the classpath; what happens if there are multiple?)? If it does load a file with that name, why doesn't it say so in the Javadoc?
你如何实际为这个<$ c $分配一个物理资源? C> ResourceReference ?有一个 getResource()
,但他们似乎错过了 setResource()
。如果您在 webapp
目录中有图像文件,那么如何附加对文件,路径甚至文件字节流的引用?内容?如果有一种方法可以在webapp中读取资源,这可能很有用,但你不能;它只在类路径中。
How do you actually assign a physcial resource to this ResourceReference
? There is a getResource()
, but they seem to have missed out setResource()
. If you have, say, an image file in your webapp
dir, how do you "attach" the reference to the file, its path, or even a byte stream of the file's contents? If there were a way to read resources in the webapp, this might be useful, but you can't; it's only in the classpath.
我希望能够挂载,例如, webapp / games / someGame.swf的内容
以便Wicket页面可以访问webapp中的SWF,或者只是对它们进行某种处理。
I would love to be able to "mount" the contents of, say, webapp/games/someGame.swf
so that the SWF in a webapp can be accessed by the Wicket pages, or just get some kind of handle on them.
推荐答案
要扩展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 :-)
这篇关于什么是Wicket中的ResourceReferences,它们如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!