Hello!
I am trying to build one of the simplest applications you can imagine. In short, it should do the following:
- At startup, recieve a file ID and some other parameters via the URL. For example ?fileId=myFile.png&foo=bar
- If the named file already exists on disk, display the contents of the file (in this case an image).
- If the named file does not already exist on disk, display an upload box for the file (.
This is all very simple, and I have a clear idea of how I would proceed in doing it. But, I ran into problems with the ParameterHandler – It seems to do its processing in handlerParameters()
after
the Application’s init() is done, meaning the parameters are not available when I need them at application startup. Below is a simple example.
Application:
package com.example.parameterhandlerapplication;
import com.vaadin.Application;
import com.vaadin.ui.*;
@SuppressWarnings("serial")
public class ParameterhandlerapplicationApplication extends Application {
@Override
public void init() {
Window mainWindow = new Window("Parameterhandlerapplication Application");
MyParameterHandler paramHandler = new MyParameterHandler();
mainWindow.addParameterHandler(paramHandler);
Label label = new Label("fileId: " + paramHandler.getFileId());
mainWindow.addComponent(label);
setMainWindow(mainWindow);
}
}
MyParameterHandler:
package com.example.parameterhandlerapplication;
import com.vaadin.terminal.ParameterHandler;
import java.util.Map;
@SuppressWarnings("serial")
public class MyParameterHandler implements ParameterHandler {
String fileId = "";
public void handleParameters(Map parameters) {
System.out.println("found: " + parameters.containsKey("fileId"));
if(parameters.containsKey("fileId")) {
fileId = ((String[]) parameters.get("fileId"))[0]
;
}
System.out.println("end: " + this.fileId);
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getFileId() {
System.out.println("get: " + this.fileId);
return this.fileId;
}
}
Output when requesting via URL ‘http://localhost:8080/ParameterHandlerApplication/?restartApplication&fileId=test’:
get:
found: true
end: test
As you can see in the output, the fileId property in MyParameterHandler hasn’t been updated at the time that init() calls getFileId() to retrieve the value of the parameter.
Now, I know it’s possible to put the code for displaying the appropriate GUI components inside handleParameters() instead of init(), but doing so feels like a non-clean way to write this code. As I see it, setting up the initial GUI should be done in the Application, not inside the parameter handler.
Indeed, I could probably put handleParameters() in the Application, but this would still be a “second phase” method that is run after the application is started, to “make things right”.
I would prefer to set up my GUI once and in one place, instead of partially in the “main” place and then having a second component adjust it, especially since this is at the very basic level of the GUI.
Seeing as one of the most common uses of URL parameters is to base decisions on their contents, am I wrong in expecting the parameters to be available at the time of setting up my GUI?
And of course, the obvious question; Is there a way to be able to access the url parameters at the time of init()? If it’s not possible using ParameterHandler, can I access them by some other means?
PS: I have been reading though all the posts I could find in the forum, but didn’t feel there is a clear answer to this, so I’m hoping this thread will be the one for that