简体   繁体   English

为什么在Tomcat的WEB-INF / classes目录中看不到图像?

[英]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. 大家好,我正在学习Vaadin for Java,我创建了一个项目,并将其作为.war导出到Tomcat webapps文件夹。 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. 现在,我重新启动了Tomcat,并在webapps文件夹中将.war解压缩到了/usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project.warproject文件夹中。

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: 我的问题是我有一个Vaadin ClassResource,它应该指向WEB-INF / classes目录中的图像(正如他们在Vaadin的书中所说),该图像位于以下路径:

/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: 但是不会显示...这是UI类的代码:

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? 为了使用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 : 我知道还有其他方法(使用ThemeResource或FileResource),但是我想使用ClassResource并按照Vaadin的书规定-> https://vaadin.com/book/-/page/application.resources.html

4.4.3. 4.4.3。 Class Loader Resources 类加载器资源

The ClassResource allows resources to be loaded from the class path using Java Class Loader. ClassResource允许使用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. 通常,相关的类路径条目是Web应用程序下的WEB-INF / classes文件夹,Java编译应在该文件夹中编译Java类并从源树复制其他文件。

The one-line example below loads an image resource from the application package and displays it in an Image component. 下面的单行示例从应用程序包加载图像资源,并将其显示在Image组件中。

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. Vaadin之书可能不太准确。 The ClassResource constructor javadoc writes: ClassResource构造函数javadoc写道:

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). 资源ID相对于使用此资源(如果使用LegacyWindow,则为应用程序)的组件的UI的位置。

So you should put the image.png next to your ImgUI.class . 所以,你应该把image.png旁边的ImgUI.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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