I have no idea why that would work for even one image. You can use ClassResource to get images from the class path, typically images that you have beside your source code. You can’t use ClassResource to get images from outside the class path (WebContent/WEB-INF/classes).
If you have put images under WebContent/images, you should use ExternalResource. You need to include the context root in the path.
ExternalResource resource = new ExternalResource("/book-examples/images/image.png");
Embedded image = new Embedded("External Image", resource);
The behavier got even stranger, i just deleted all images from the images Folder but logo1.jpg is still showing even after refreshing the cache with strg + f5
I was searching for the image in the deploy folder but didn´t find any, so i expected that nothing is loaded.
Maybe i have a problem with the server configuration?
ExternalResource resource = new ExternalResource("/kwfms_v0001/images/kremer_logo.jpg");
Embedded logo = new Embedded("", resource);
header.addComponent(logo)
I think in this case i have to do some mapping in web.xml of the Application here is my web.xml as it is right now:
// Images Folder is located here /WebContent/images
// i get the absolute path of the images folder and add the name of the image
String path=context.getHttpSession().getServletContext().getRealPath("/images")+ "/mylogo1.jpg";
// i create a simple FileResource
FileResource resource = new FileResource(new File(path),this);
Embedded logo = new Embedded("mylogo", resource);
header.addComponent(logo);
works fine until now
now my Question:
Can i expect any problems from my solution? Or why does nobody recommended to do like this?
Sure, using a FileResource is just as fine. That way you don’t have to open up the servlet URL for the image directory with a /foo/* for the url-pattern.
Using getApplication().getContext().getBaseDirectory().getAbsolutePath() to get the base directory of the application content may sometimes be a bit easier.
actually I am trying to display an image in my portlet and I am using following code inside the init() method:
ClassResource resource = new ClassResource("image_with_text_477x120.jpg", getMainWindow().getApplication());
Embedded image = new Embedded("Yes, logo:",resource);
layout.addComponent(image);
In the portlet, image is not displayed but i can see the text “Yes, logo:”… any idea how to do this? image is in my portletRoot/WEB-INF/classes directory. I am using Liferay and Tomcat…
Regarding your other question, you can get the context from the application object with getContext(). The ApplicationContext interface doesn’t have the getHttpSession() method that Ingo used, but you have to cast it to WebApplicationContext. This only works in a regular server; the context class different under a portal or GAE.
Same code works now if i place it outside init() method. I created a Form, added couple of Panels to the Form. In first panel i used the same code (above one) and in the 2nd Panel i created some form fields. This Form is then added to the main window in the init() method and the image gets loaded perfectly. But for some reason, image does not gets displayed if added directly to the main window in init() method. This solution works for me though as the image(which is the logo) is being loaded as part of the initial UI.