开发者

Help me understand JSF managed bean lifecycle

I have a form bean defined in session scope and a controller bean defined in request scope. I am using JSF to inject the session scoped bean in to the request scoped bean.

<managed-bean>
<managed-bean-name>Controller</managed-bean-name>
<managed-bean-class>
  com.mycomp.Controller
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>form</property-name>
<value>#{Form}</value>
</managed-property>
</managed-bean>

<m开发者_Go百科anaged-bean>
  <managed-bean-name>Form</managed-bean-name>
   <managed-bean-class>com.mycomp.form</managed-bean-class>
   <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

The problem is that when I save the form my persistence layer returns a new bean with ID, create date, and other attributes set. I then set the local bean using the returned bean. Should I also set the session scoped bean at this time?

class Controller {

  private Form form;
  // getters and setters here
  ...

  public void save() {
    Form f = dataservice.save(form);
    this.form = f;

    // This
    //FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("Form", form);

    // or this?
    //FacesContext.getCurrentInstance().getApplication().createValueBinding(
    //          "#{Form}").setValue(
    //          FacesContext.getCurrentInstance(), form;
    }       
  }


You don't need to do anything. The changes are already reflected in the 'form' bean. So you can basically go ahead with just

public void save() {
    dataservice.save(form);
}

assuming that the dataservice is doing its job well enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜