大家好,我正在学习 Java 版 Vaadin,我创建了一个项目并将其作为 .war 导出到 Tomcat webapps 文件夹。该项目位于此路径:
/usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project.war
现在我已经重新启动了 Tomcat,并且在 webapps 文件夹中,.war 被解压到 /usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project.warproject
文件夹中.
我的问题是我有一个 Vaadin ClassResource,它应该指向 WEB-INF/classes 目录中的一个图像(正如他们在 Vaadin 书中所说),图像位于这个路径:
/usr/local/apache-tomcat/apache-tomcat-8.0.15/webapps/project/WEB-INF/classes/image.png
但是没有显示...这是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));
}
}
要使用 ClassResource 显示图像,我应该怎么做?我知道还有其他方法(使用 ThemeResource 或 FileResource),但我想使用 ClassResource 并且正如 Book of Vaadin 所述 -> 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")));
为什么在我的情况下它不起作用?我该怎么办?
感谢关注!
最佳答案
可能 Vaadin 之书不太准确。 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).
所以你应该把 image.png
放在你的 ImgUI.class
旁边。
关于java - 为什么我在 Tomcat 的 WEB-INF/classes 目录中看不到图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27333960/