skiabox
skiabox

Reputation: 3507

How to display images using vaadin ClassResource class and Tomcat as web server?

I have created a Vaadin application that tries to use the ClassResource vaadin class to load some icons along with some vaadin components but I cannot see the icons in the final application. In icons place I see a blue question mark. The application is deployed on tomcat (latest version). Here is part of the code.

IconsUI.java :

tf.setIcon(new ClassResource("email.png"));
cb.setIcon(new ClassResource("note.png"));
ta.setIcon(new ClassResource("document.png"));
bt.setIcon(new ClassResource("ok.png"));

These image files are located in the same package as the IconsUI.java class.

My web.xml is the following :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

    <display-name>icons</display-name>
    <context-param>
        <description>
            Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet>
        <servlet-name>Icons Application</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <description>
                Vaadin UI class to use</description>
            <param-name>UI</param-name>
            <param-value>MavenVaadinIcons.IconsUI</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Icons Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

Upvotes: 2

Views: 4913

Answers (2)

Lahiru Chandima
Lahiru Chandima

Reputation: 24068

To load an image using ClassResource, your images should be available in the classpath of your final web application. You can add your images to classpath by placing the images in src/main/resources folder of your maven source.

Also, you need to add a leading / character to image file names. The ClassResource constructor you used doesn't have a Class argument, so it uses the current UI class as the class. The resource you specified is looked up in the same package as the UI class. By adding a leading / character, the file will be looked up in the classpath root.

tf.setIcon(new ClassResource("/email.png"));
cb.setIcon(new ClassResource("/note.png"));
ta.setIcon(new ClassResource("/document.png"));
bt.setIcon(new ClassResource("/ok.png"));

Upvotes: 4

Henri Kerola
Henri Kerola

Reputation: 4967

Because you use Maven, you must place resource files into the src/main/resources folder. If you don't have that folder, you can create it.

If your image's path is for example src/main/java/com/example/email.png put it into a corresponding package in resources: src/main/resources/com/example/email.png.

Upvotes: 1

Related Questions