How to get a static object(s)

Hi,
I’m new both in Vaadin and Eclipse, so please forgive me for such an elementary question. I got two problems:

  1. I want to have a static logout page logout.html in project named VaadinHelloWorld in folder /static. The logout process will be initiated through pressing a Button with his own listener:
	
public void thirdButtonClick (Button.ClickEvent event) {
      setLogoutURL("/static/logout.html");
      System.out.println(mainWindow.getApplication().getLogoutURL());
      mainWindow.getApplication().close();
}

(Just for proof, I got every time the text “/static/logout.html” in console after pressing the button.)
I have made a folder named “static” under the project and placed a static html file named logout.html. However, every time i get an error 404 with “The requested resource (/static/logout.html) is not available.” message. So, what’s the catch? Should I place the folder “static” place somewhere else?

  1. my second problem is also in relation with the static resorces. This time, I have tried to load an image as a logo. The class -the only one what I currently use - is in package com.example.vaadinhelloworld as follows:

package com.example.vaadinhelloworld;
import ....
public class VaadinhelloworldApplication extends Application implements
		com.vaadin.ui.Button.ClickListener {...}

The code which WORKS for getting the image is :


ClassResource res = new ClassResource("/com/example/vaadinhelloworld/pics/logo.png", mainWindow.getApplication());
mainWindow.addComponent(new Embedded ("",res));

Of course, I get the proper directory structure under Java resources/src with the file logo.png. It works fine, but I’m not able to place the picture somewhere else, for example into folder “static” mentioned above. If I do so, I will get no error, no exception, just the picture does not appear. I feel that the catch may be the same as above, but I’m not sure. Anyway I wil greatly appreciate any hint.
Greetings, Kornel

Hi, and welcome! :slight_smile:

Static content is always served from the WebContent folder in your project root, so you should move your static folder here. Everything else is hidden.

There are a few ways to make this happen: You can use an ExternalResource to serve the image from your static folder, or a ThemeResource to serve it from a theme folder. In the latter case, you need to create your own theme and theme folder (this is very easy if you just extend the default Vaadin theme, Reindeer. There is a chapter about this in the Book of Vaadin) and set the theme name for applications.

Hope this helps.

Hi, I tried the hints, they do not work. For logout page i get still 404, and for resorce (this time Externalresource) i get no picture, no error, no exception.
The folder structure is shown on next picture:

http://picpaste.com/webcontent-VwBcVyNk.JPG
Any idea ?

new Embedded("", new ExternalResource("static/logo.png"))

That should work for the image.

As for the logout, this worked for me:

	@Override
	public void init() {
		...
		setLogoutURL("static/logout.html");
	}

Note, these can behave differently depending on your servlet mappings.

Hi,
it does not work. Anyway, here’s the web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>VaadinHelloWorld</display-name>
	<context-param>
		<description>
		Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>false</param-value>
	</context-param>
	<servlet>
		<servlet-name>Vaadinhelloworld Application</servlet-name>
		<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
		<init-param>
			<description>
			Vaadin application class to start</description>
			<param-name>application</param-name>
			<param-value>com.example.vaadinhelloworld.VaadinhelloworldApplication</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Vaadinhelloworld Application</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

But even if it’s wrong, the direct access like http://localhost:8080/static/logout.html or http://localhost:8080/WebContent/static/logout.html should work, despite the (potentially) incorrect web.xml, right? So it seems that could be a Tomcat instance conf. issue, what do you think?

Yeah, a /* mapping will most likely swallow all requests to any static content; the reason it worked in my case was that i had the following mapping:

  <servlet-mapping>
  	<servlet-name>Test Application</servlet-name>
  	<url-pattern>/demo/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Test Application</servlet-name>
    <url-pattern>/VAADIN/*</url-pattern>
  </servlet-mapping>

I played around with this a while, but couldn’t get the resources to load without mapping the application to a non-root url… Maybe someone else can chime in here?

Hi,

it was taken a day or so to realise the catch: you must have /VAADIN/* mapping
mandatory
in such cases, the /static/* mapping itself does not work!
After some googlework, it seems that others had similar problems:

Adding static content

Anyway, I wish you to say thank for your precious help.