sina
sina

Reputation: 1829

Apache Wicket does not load (or display) static image

I am currently working my way into wicket and developed a small demo application. Now I want to display a static image in that application (to be more specific, this image should be added to a panel which is then added to the home page).

This is what I have so far:

1) The HTML code for the panel that should hold the image. There is no specific wicket code present, it should only hold an image present in the project structure.

<html><body>
  <wicket:panel>
    Just some static text inside the image panel...<br/>
    <img src="test.jpg"></img>
  </wicket:panel>
</body></html>

The panel is called ImagePanel and all three files (ImagePanel.java, ImagePanel.html, test.jpg) are located in the same package.

2) The Java code for the ImagePanel - rather short, as there is nothing really to do:

public class ImagePanel extends Panel {
    public ImagePanel(String id) {
        super(id);
    }
}

3) The HTML code for the homepage:

<html><body>
  <div wicket:id="dynamicComponent"></div>
</body></html>

4) And the Java code for the homepage:

public HomePage() {
    add(new ImagePanel("dynamicComponent"));
}

Please note the following: In practice, HomePage.java and ImagePanel.java are located in different OSGi-projects. The code for the HomePage is simplified here, in practice the ImagePanel is located via a service and dynamically added. This works so far, I see the static text. But the image is not loaded, I just get this broken-image-thumbnail.

Like I said, all three ImagePanel-related files (.java, .html, the picture) are located in the very same package. I can access the Panel itself from the homepage-project as I can see the static text. But I can't get the image to display.

The Application Server I use is Jetty, started from inside the project that holds the homepage.

Update: I've narrowed the problem to the following: Everything works fine as long as I only display static text inside of ImagePanel.html. As soon as I want to add the image (even if inside of a wicket:link-tag as suggested below) I end up with the following exception:

[qtp401543768-20] WARN org.apache.wicket.core.util.lang.WicketObjects - Could not resolve class [plugin.ImagePanel]
java.lang.ClassNotFoundException: plugin.ImagePanel

So without the image the server bundle is able to load the class from the gui bundle and display the static text. With the image I end up with a ClassNotFoundException for the same class.

Find the whole stack trace here: stack trace

Upvotes: 0

Views: 2747

Answers (2)

sina
sina

Reputation: 1829

Thanks for the help, though meanwhile I managed to solve the problem myself. It was not a sole wicket problem but also some difficulties with the OSGi-bundle-setup.

As I mentioned before, the ImagePanel is located in a different OSGi bundle than the HomePage. The problem arised as all Wicket-libraries are located in the HomePage-bundle (I get Linker errors if both bundles use their own libraries). Therefore loading an image in the ImagePanel used classes located in the HomePage-bundle who now couldn't access the image.

I solved the problem by introducing an OSGi-Service in the ImagePanel-bundle that passes all used images as byte arrays to the HomePage-bundle. The HomePage-bundle has now access to all resources and registers them as SharedResource with an id given again by the ImagePanel-bundle.

After registration, it is no problem to access the images in the ImagePanel-bundle via SharedResourceReference.

Upvotes: 0

HaMMeReD
HaMMeReD

Reputation: 2462

In your template, put a around

 <wicket:link>
   <img src="test.jpg"></img>
 </wicket:link>

Wicket will rewrite the url so that the image works.

If you have a dynamic Image that you want to load a Package Resource, you use PackageResourceReference.

e.g.

add(new Image("myImage", new PackageResourceReference(MyClass.class, "test.jpg"));

Upvotes: 4

Related Questions