I decided to build a working version of this functionality using JSF and PrimeFaces. Here is the full code for my working example:
Facelets XHTML with JavaScript
Code: Select all
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>HTML5 Local Storage</title>
<script>
function loadLocalStorage() {
var prefix = 'prefix';
getLocalStorageValue(prefix, 'firstName');
getLocalStorageValue(prefix, 'surname');
getLocalStorageValue(prefix, 'dateOfBirth_input');
getLocalStorageValue(prefix, 'notes');
}
function clearLocalStorage(prefix) {
localStorage.removeItem(prefix);
// alert(localStorage.getItem(prefix));
}
function getLocalStorageValue(prefix, elementId) {
if (localStorage.getItem(prefix)) {
var element = document.getElementById(elementId);
if (element.value === '') {
var prefixValue = JSON.parse(localStorage.getItem(prefix));
if (prefixValue[elementId]) {
element.value = prefixValue[elementId];
}
}
}
}
function setLocalStorageValue(prefix, elementId) {
if (!localStorage.getItem(prefix)) {
localStorage.setItem(prefix, JSON.stringify({}));
}
var element = document.getElementById(elementId);
var prefixValue = JSON.parse(localStorage.getItem(prefix));
prefixValue[elementId] = element.value;
// alert(JSON.stringify(prefixValue));
localStorage.setItem(prefix, JSON.stringify(prefixValue));
}
</script>
</h:head>
<h:body>
<h:form prependId="false">
<p:panelGrid columns="2">
<f:facet name="header">
<h:outputText value="Registration Form"/>
</f:facet>
<p:outputLabel for="firstName"
value="First Name:"/>
<p:inputText id="firstName"
widgetVar="firstName"
onchange="setLocalStorageValue('prefix', 'firstName')"
value="#{registrationBean.firstName}"/>
<p:outputLabel for="surname"
value="Surname:"/>
<p:inputText id="surname"
widgetVar="surname"
onchange="setLocalStorageValue('prefix', 'surname')"
value="#{registrationBean.surname}"/>
<p:outputLabel for="dateOfBirth"
value="Date of Birth:"/>
<p:calendar id="dateOfBirth"
widgetVar="dateOfBirth"
onchange="setLocalStorageValue('prefix', 'dateOfBirth_input')"
value="#{registrationBean.dateOfBirth}"/>
<p:outputLabel for="notes"
value="Notes:"/>
<p:inputTextarea id="notes"
widgetVar="notes"
onchange="setLocalStorageValue('prefix', 'notes')"
value="#{registrationBean.notes}"/>
<f:facet name="footer">
<div align="right">
<p:commandButton type="reset"
value="Reset"
onclick="clearLocalStorage('prefix')"/>
<p:commandButton value="Register"
onclick="clearLocalStorage('prefix')"/>
</div>
</f:facet>
</p:panelGrid>
</h:form>
</h:body>
</html>
Code: Select all
package jsf;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
@Named(value = "registrationBean")
@RequestScoped
public class RegistrationBean {
private String firstName;
private String surname;
private Date dateOfBirth;
private String notes;
public RegistrationBean() {
// firstName = "Default";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
@PostConstruct
public void init() {
if (!FacesContext.getCurrentInstance().isPostback()) {
RequestContext.getCurrentInstance().execute("loadLocalStorage()");
}
}
}
Code: Select all
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>HTML5 Local Storage</title>
</h:head>
<h:body>
<h:form prependId="false">
<p:panelGrid columns="2">
<f:facet name="header">
<h:outputText value="Registration Form"/>
</f:facet>
<p:outputLabel for="firstName"
value="First Name:"/>
<p:inputText id="firstName"
widgetVar="firstName"
localStorage="prefix"
value="#{registrationBean.firstName}"/>
<p:outputLabel for="surname"
value="Surname:"/>
<p:inputText id="surname"
widgetVar="surname"
localStorage="prefix"
value="#{registrationBean.surname}"/>
<p:outputLabel for="dateOfBirth"
value="Date of Birth:"/>
<p:calendar id="dateOfBirth"
widgetVar="dateOfBirth"
localStorage="prefix"
value="#{registrationBean.dateOfBirth}"/>
<p:outputLabel for="notes"
value="Notes:"/>
<p:inputTextarea id="notes"
widgetVar="notes"
localStorage="prefix"
value="#{registrationBean.notes}"/>
<f:facet name="footer">
<div align="right">
<p:commandButton type="reset"
value="Reset"
localStorage="prefix"/>
<p:commandButton value="Register"
localStorage="prefix"/>
</div>
</f:facet>
</p:panelGrid>
</h:form>
</h:body>
</html>
I would love some feedback if anyone is interested.