简体   繁体   中英

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

Hi everyone I am learning Vaadin for Java, I have created a project and exported it as .war to the Tomcat webapps folder. The project is located at this path:

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

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

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

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

But it is not displayed... Here is the code of 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 in order to display the image using ClassResource? I know there are other methods (using ThemeResource or FileResource), but I would like to use ClassResource and as the Book of Vaadin states -> https://vaadin.com/book/-/page/application.resources.html :

4.4.3. Class Loader Resources

The ClassResource allows resources to be loaded from the class path using Java Class Loader. Normally, the relevant class path entry is the WEB-INF/classes folder under the web application, where the Java compilation should compile the Java classes and copy other files from the source tree.

The one-line example below loads an image resource from the application package and displays it in an Image component.

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

Why in my case it is not working? What should I do?

Thanks for the attention!

Probably the Book of Vaadin not very accurate. The ClassResource constructor javadoc writes:

Creates a new application resource instance. The resource id is relative to the location of the UI of the component using this resource (or the Application if using LegacyWindow).

So you should put the image.png next to your ImgUI.class .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM