What are ResourceReferences in Wicket and how do they work?

I saw examples containing things like this:

mountSharedResource("/images/logo.gif", new ResourceReference(ImageScope.class, "logo.gif").getSharedResourceKey()); mountSharedResource("/resource", Application.class.getName() + "/" + resourceKey); 

But Javadoc says this for the constructor:

 ResourceReference(java.lang.Class<?> scope, java.lang.String name); 

So, when you create a ResourceReference , you give it a class. What for? Usually you need the global scope or scope of the ResourceReference object you just created, not?

Also, what is name ? Is this a sharedResourceKey ? If not, where does resourceKey come from? How does this happen and why is it not a name? Or name looked through the class path and magically loaded (assuming there is only one file with that name in the class path, what happens if there are several?)? If he uploads a file with that name, why doesn't he talk about it in Javadoc?

How do you assign a physical resource to this ResourceReference ? There is getResource() , but they seem to have missed setResource() . If you have, say, an image file in the webapp directory, how do you β€œattach” a link to the file, its path, or even a stream of bytes of the contents of the file? If there was a way to read resources in webapp, this might be useful, but you cannot; it is only on the way to classes.

I would like to "mount" the contents of, say, webapp/games/someGame.swf so that SWF in webapp can access the Wicket pages or just get some kind of handle to them.

+6
source share
2 answers

To expand Andrew's answer:

A ResourceReference as such is a reference to a resource accessible through SharedResources . Any type of Resource that you add to SharedResources (usually executed in Application#init() ) has a name that you define. Any Component that uses a resource can then reference this shared resource through a ResourceReference with this name - hence a parameter called "name". In this case, the area parameter (class) is not required.

This is a common case to reference any resource.

The case shown in the examples of yours and Andrew's is more specific: your ResourceReference name does not apply to the Resource previously added to SharedResources . Here, the so-called PackageResource lazily initialized and added to SharedResources .

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

So, if you just want to refer to a file as an image from your class path, the Andrew example is just a very useful shortcut to avoid creating this PackageResource yourself. As noted above, there is more to ResourceReference than just :-)

+3
source

A resource, such as an image, is usually associated with a specific web page. Therefore, it makes sense to find this image in the same place as the Java and HTML files.

The class parameter serves as the basis for finding your resource. The second parameter of the ResourceReference constructor is the name of the resource relative to the directory containing the class.

So, for example, you could -

 new ResourceReference(AClass.class, "sub/directory/image.jpg"); 

You assign a physical resource by simply placing that resource in the correct directory when you deploy your application.

There is a chapter on the use of resources in the book Wicket in Action.

+10
source

Source: https://habr.com/ru/post/892712/


All Articles