Why can't I see the image inside the WEB-INF / classes directory in Tomcat?

Hi everyone I am learning Vaadin for Java, I created a project and exported it as .war in Tomcat webapps folder. The project is on this path:

/usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project.war

Now I restarted Tomcat and inside the webapps.war folder it is unpacked into a folder /usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project.warproject

.

My problem is that I have a Vaadin ClassResource that needs to point to an image inside the WEB-INF / classes directory (as the Book of Vaadin says), the image is located in this path:

/usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project/WEB-INF/classes/image.png

But it doesn't show up ... Here is the code for the UI class:

public class ImgUI extends UI {

     protected void init(VaadinRequest request) {

        // initializing the layout object etc...

        Resource r = new ClassResource("image.png"); // the image which is inside WEB-INF/classes dir
        layout.addComponent(new Image("Class resource image", r));

     }
}

      

What should I do to display an image using the ClassResource? I know there are other methods (using ThemeResource or FileResource), but I would like to use ClassResource and as stated in the Vaadin book -> https://vaadin.com/book/-/page/application.resources.html :

4.4.3. Classloader resources

The Resource class allows you to load resources from the classpath using the Java Class Loader. Usually the corresponding classpath entry is the WEB-INF / classes folder in the web application, where Java compilation is to compile Java classes and copy other files from the source tree.

The single line example loads an image resource from an application package and displays it in an Image component.

layout.addComponent(new Image(null,
        new ClassResource("smiley.jpg")));

      

Why doesn't it work in my case? What should I do?

Thanks for your attention!

+3


source to share


1 answer


The Book of Vaadin is probably not very accurate. The ClassResource

javadoc constructor writes:

Creates a new instance of the application resource. The resource ID refers to the location of the UI component using that resource (or application, if LegacyWindow is used).



So, you have to place image.png

next to yours ImgUI.class

.

0


source







All Articles