How to load a file on the server side

Hi,

When i want add a little image into a button, i use this code which works fine :

mybouton.
setIcon
(new ThemeResource("…/my_theme/myImage/fr.gif "));

But Vaadin needs to know the first part of the path, which is missing here, in order to locate the resource.
So, how Vaadin gets the path which is before …/my_theme/myImage/fr.gif ??

the
setIcon
meythod belongs to the AbstractComponent class, the source code is here, but i don’t see any explanation about it :

AbstractComponent source code


Vaadin javadoc

Could someone explain where the first part of the path, comes from ?

Eric

Hi,

The theme resource path is relative to the path of the current theme. The theme path is [tt]
VAADIN/themes/themename
[/tt] under the web application content. When deployed, the complete URL to the [tt]
styles.css
[/tt] file of a theme would be, for example, [tt]
http://localhost:8080/myproject/VAADIN/themes/mytheme/styles.css
[/tt].

You can use relative paths to point to resources in other themes.

The ThemeResource is probably best documented in
its book section
and
its example
. It’s also mentioned in the API documentation for
setIcon()
.

Thank you Marko,

I need to load a csv file named “eric.csv” into a BufferedReader.
This file is located here : WebContent / VAADIN / themes / eric_theme

If i use the following code, i use the path of this file, but do not know how to get it.

BufferedReader input = new BufferedReader( new InputStreamReader(new FileInputStream(pFilePath), “UTF-8”));

From your point of view, what is the best way to load this file ?

Eric

You should normally not put application data files in the theme folder, as they can be read by browser and are therefore a security risk. The WEB-INF folder is a safer place.

You can get the base directory in which the application is deployed with:

String basepath = getApplication().getContext().getBaseDirectory().getAbsolutePath();

The WEB-INF folder would be basepath+“/WEB-INF”, the themes would be in basepath+“/VAADIN/themes/mytheme”, and so forth.

You can also put static data files under the WEB-INF/classes folder and then read them with the Java class loader.

Notice that
non-static data
should not be kept anywhere in the web application, as redeploying the application would cause deletion of the data, depending on the web server.


public class Launcher extends Application{
     public void init() {
          [color=#14a235]
ApplicationContext context = this.getContext();
[/color]
	  if (context instanceof WebApplicationContext) {
       	          System.out.println("Path to WEB-INF : "+[color=#14a235]
context.getBaseDirectory().getAbsolutePath()
[/color]);
	  }
	final Window mainWindow = new Window("Myproject Application");
	setMainWindow(mainWindow);
	mainWindow.setSizeFull();
	mainWindow.addComponent(new WindowOpener("Window Opener", mainWindow));
}

I compile this code inside Eclipse from this location :

C:\DEV\Workspace_Vaadin\myVaadinProject
, and i get this strange path with
.metadata
:

C:\DEV\Workspace_Vaadin

.metadata

.plugins\org.eclipse.wst.server.core\tmp2\myVaadinProject

which contains WEB-INF and compiled classes.

I tried to put my csv file into

C:\DEV\Workspace_Vaadin\myVaadinProject\WEB-INF
,
and after running the project, the file was loaded and copied into

C:\DEV\Workspace_Vaadin

.metadata

.plugins\org.eclipse.wst.server.core\tmp2\myVaadinProject\WEB-INF

So it is a strange behaviour, but it works fine.

Thank you Marko :slight_smile:

Yes, when you deploy the project to a server in Eclipse, it deploys the project to such odd location.