Disclaimer: Serializing a bean in a URL is not such a great idea for GWT. I found out that if you need to put data at a URL, it should be as small as possible and only what is needed to restore the state of your page. See how Gmail uses its history tokens and you will see that it is very small.
With this disclaimer:
For the GWT project I was working on, I simply wrote out the bean values separated by a separator. When you read the values back, I used the String.split () method to get the array. With this array, I am returning the values back to the right bean properties. In code:
public class Sample { private int a; private boolean b; private String c; //getters and setters for fields not shown public String toHistoryToken(){ return a+"/"+b+"/"+c; } public void fromHistoryToken(String token){ String[] values=token.split("/"); a=Integer.parseInt(values[0]); b=Boolean.parseBoolean(values[1]); c=values[2]; } }
For more complex scenarios, you may have to do more complex things. For example, for nested objects, you need to write code to pass values to a child object.
Also, keep in mind that you must ensure that any values you use do not contain a separator. Therefore, if you know that your lines may contain "/", you may need to perform a replace () operation to avoid any nested delimiters.
Peter Dolberg
source share