0

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_METHODis set to client)
  • 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 member myObject 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...

MichaelRS
  • 51
  • 3
  • Does a session scoped bean survive a restart? – Kukeltje Dec 12 '19 at 19:47
  • Does https://stackoverflow.com/questions/24394142/restoring-request-parameters-in-viewscoped-bean-after-session-expires help? – Kukeltje Dec 12 '19 at 19:51
  • For me when using Tomcat and OpenWebBeans, while restarting the Tomcat sessions get persisted and restored. The @PostConstruct is not invoked again for viewScoped beans, irregardles of state_saving_method server|client. – Selaron Dec 13 '19 at 10:48

1 Answers1

0

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 @PostConstruct and 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).

@PostConstruct
public void onPostConstruct() {

  if (Faces.isPostback() && this.myObject == null) {
    // Load object from database
    final long requestId = Long.valueOf(Faces.getRequestParameter("id"));
    setMyObject(objectService.get(requestId));
  }
}

Is this a correct assumption: When I would have had some additional validators on the view parameter id they do not to have checked explicitly in @PostConstruct because the postback request transports the "original" id-value, so this must be a previously validated value, so it cannot be tampered by the user?

MichaelRS
  • 51
  • 3