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
Controlleris@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_METHODis set toclient) - When the server is up again, the user wants to submit the form
- In that case the controller bean's
@PostConstructwill be called, but as expected the membermyObjectwill 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...
Yes, the same behavior on a @SessionScoped bean and the server is cleaned and restarted. Thanks for the link, it helped a lot.
It seems to me that the only solution is to hook into
@PostConstructand check if the member is null. In that case I simply have to re-initialize the view by myself (which would be by default done using a converter).Is this a correct assumption: When I would have had some additional validators on the view parameter
idthey do not to have checked explicitly in@PostConstructbecause the postback request transports the "original"id-value, so this must be a previously validated value, so it cannot be tampered by the user?