|
StorageApi
IntroductionThe Storage API (download) leverages the W3C Web Storage API. See this article for a nice introduction to the sessionStorage. With this API you are able to store data on the client using a Java Map-like interface (key/values). Browser supportNot every browser supports this feature, and currently there are no fallback scenario's to rely on Flash, Java or Gears plugins. The following browsers are supported:
Other browsers might also support Web Storage, we just didn't test them nor found any feature support documentation. UsageGet the Storage API using MavenAdd the repository and the following to your pom.xml file: <dependencies>
...
<dependency>
<groupId>com.google.code.gwt-mobile-webkit</groupId>
<artifactId>gwt-html5-storage</artifactId>
</dependency>
</dependencies>Add the Storage API to your projectIf you don't use Maven, copy the gwt-html5-storage-x.x.x.jar file from the downloaded distribution to your project classpath. This jar contains everything you need to use the Storage API. Next, in your GWT module gwt.xml file (which uses the Storage API), add the following entry: <inherits name="com.google.code.gwt.storage.Html5Storage" /> This imports the Storage API into your module. Now you are ready to use the API! is API supported?The following will test whether the API is supported in your browser: if (Storage.isSupported()) {
// Interact with the storage...
}Getting a Storage instanceThere are two Storage types available. Both have exactly the same API, they just persist differently:
A Storage is obtained like this: Storage localStorage = Storage.getLocalStorage(); Storage sessionStorage = Storage.getSessionStorage(); Writing in the StorageValues can be written as follows: localStorage.setItem("key", "value");All keys and values must be Strings. Reading the StorageString value = localStorage.getItem("key");Iterating over the Storagefor (int i=0; i<localStorage.getLength(); i++) {
String key = localStorage.key(i);
String value = localStorage.getItem(key);
// ...
}Accessing the Storage using a Java MapFor convenience sake we added a Java Map implementation over the Storage API. Using this Map you can read and write the Storage using familiar methods: // Creating a Map instance using the localStorage: Map<String, String> map = new StorageMap(localStorage); // Invoke some Map method: map.putAll(otherMap); // ... Word of advice: Please note that the Java Map interface is way bigger than the Storage interface, and introducing the StorageMap to your code implies some overhead in accessing the Storage data. Only use the StorageMap if absolutely necessary. |
Should work on Chrome 4.0 as well.
Firefox (WIN) 3.6.10 Does not like integers as key values when using. Works greate in Chrome (WIN) 8.0.552.0 dev, Safari (WIN) 5.0.2, Opera (WIN) 10.63.
Example: if (Storage.isSupported()) {
}Gives: "com.google.gwt.core.client.JavaScriptException?: (NS_ERROR_DOM_INDEX_SIZE_ERR): Index or size is negative or greater than the allowed amount" in hosted mode with firefox.
And so it seems!
http://firefox.sourcearchive.com/documentation/3.6plus-pnobinonly-0ubuntu5/nsDOMStorage_8cpp-source.html
Apparently, this seems to work 'as designed' according to Firefox. Feeding the API with an integer key is interpreted as an indexed value.
Prefix with an underscore (e.g. "5") should be a valid workaround.
I'm not using Mavern. I downloaded the jar to my project under a folder called "libs". Right clicked on the jar file and added to my build path. Then modified the gwt.xml file to add the inherit line. However I keep getting this error:
Unable to find com/google/code/gwt/storage/Html5Storage?.gwt.xml on your classpath ....
What am I doing wrong?