Supposing following scenario:
- In a web application I have a simple form on a page where the user can update a value in a database; the backing bean
Controller
is@ViewScoped
. The object which holds the value is read from database in a custom converter when the page is loaded using a GET parameter.
<!-- The details page for editing an object -->
<f:metadata>
<o:viewParam name="id" value="#{controller.myObject}" required="true">
<f:converter converterId="myObjectConverter" />
</o:viewParam>
</f:metadata>
<o:form includeViewParams="true">
<h:inputText id="desc" value="#{controller.myObject.myValue}" validatorMessage="Input invalid">
<f:validateRequired />
<f:validateLength minimum="1" />
</h:inputText>
<p:message for="desc" />
<h:commandButton action="#{controller.updateValue()}">
Save
</h:commandButton>
</o:form>
@Named
@ViewScoped
public class Controller implements Serializable {
// Getter/setter omitted
private MyObject myObject;
@PostConstruct
public void onPostConstruct() {
}
public String updateValue() {
// Persist myObject and return to list page
return "objects.xhtml?faces-redirect=true&id=" + myObject.getId();
}
}
- Now the user navigates to this page, but does not click the button to submit the form and keeps the browser window open
- Meanwhile the server restarts (note:
javax.faces.STATE_SAVING_METHOD
is set toclient
) - When the server is up again, the user wants to submit the form
- In that case the controller bean's
@PostConstruct
will be called, but as expected the membermyObject
will not be set by the converter resulting in NullPointer access.
So is there any suggested JSF-way to handle such a scenario? I hope I have expresses myself clearly...