Load an image from WEBAPP folder

Hi,

I’m reading the Vaadin book and trying to understand how resources work.

From what i could understand, ClassResource load resources in the classpath.

FileResource is the better option for “full path” resources.

And ThemeResource for resources related to a theme.

So, given these options which is the best for load resources in webapp folder? Is there another?

You may ask “why webapp folder”? Because I think it is the most intuitive place for images and other resources. At least it was where I usually put my resources in other projects.

PS: I mean webapp in maven (src/main/webapp) which is the same of WebContents in Eclipse webapp project.

Thanks.

Hi!

If you want to put your images in your webapp folder it’s probably easiest to use ExternalResource and give it the URL to your image, which will be in the form http://server/context/path/to/image.png

It’s usually a bigger pain to use the ClassResource or FileResource. ThemeResource is very easy to use, but it requires you to duplicate images if you happen to switch between two different themes.

HTH,
/Jonatan

Hello, thanks for the response.

Your idea is pretty good except for the fact that, from what I could understand, Vaadin’s servlet doesn’t exposes the contents of the webapp folder.

I have a servlet that is configured to answer any request.

@WebServlet(urlPatterns = “/*”)
public class VaadinAppServlet extends AbstractApplicationServlet { }

So, when i put http://localhost:8080/context/image.png on the browser (or ExternalResource), the application loads again.

I have managed to find that the VAADIN folder (http://localhost:8080/context/VAADIN/image.png) is exposed to the “outside world”, but I’m not sure if I’m doing the right thing since I’m not putting the files inside a specific theme.

I am facing a similar issue.

I have an image in the Project/WebContent/images folder and trying to access it through

ExternalResource but the application loads again if I hit the url : http://localhost:8080/Application/images/Image.png.

Any help?

Hi,

Try adding something like this to your web.xml descriptor

  <servlet-mapping>
  	<servlet-name>default</servlet-name>
  	<url-pattern>/images/*</url-pattern>
  </servlet-mapping>

This makes tomcat (or whatever servlet container you’re using) serve the specified url-pattern statically instead of going through the vaadin application that is mapped to /* by default.

HTH,
/Jonatan

Thanks a lot. This actually has moved it somewhere, now I get the Http 404 Resourcce not found error when I hit the URL http://localhost:8080/Application/images/Image.png inspite of the fact that the image is present in the webapps folder of tomcat - webapps/Application/images.