In my application, I have the following beans:
@Named(value = "mrBean")
@SessionScoped
public class MrBean implements Serializable {
@EJB
private MrsBean mrsBean;
private Item item;
public void updateItem() {
this.item = mrsBean.updateItem(item.getId());
}
}
@Named(value = "itemBean")
@RequestScoped
public class itemBean {
@Inject
private MrBean mrBean;
@PostConstruct
public void init() {
if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("update") != null) mrBean.updateItem();
}
}
Before showing the item’s information on the ViewItem.xhtml page, I will check if the update parameter is submitted to update the item before showing.
When I test the page with the parameter update=true, I have no idea why the old data was rendered instead of the new updated one. In fact, I have to refresh the page before the new data is rendered.
From the above result, I wonder if the @PostConstruct method was called after the view was rendered.
I’d be very grateful if you could give me an advice.
Best regards,
That can happen if
#{itemBean}is been referenced for the first time in the view after#{mrBean.item}. E.g.The managed beans are constructed for the first time when an EL expression references it for the first time and it does not exist in the scope yet. On a GET request wherein the managed bean instance is not referenced by a taghandler attribute or a
f:event, then it would only be constructed during the render response.If rearranging them so that the
#{itemBean}is always referenced (and thus constructed) before#{mrBean.item}is been referenced is not possible, then better make the@PostConstructof#{itemBean}a<f:event type="preRenderView">instead.