0

I have two booleans that controls the render of some components, the problem is that the variables saves there last state until the session expires, i

<f:facet name="footer">
  <p:commandButton action="#{personBean.report}" ajax="false" value="Publish"
                            rendered="#{personBean.reportMode}" icon="ui-icon-check" />
  <p:commandButton action="#{personBean.saveEditing}" ajax="false" value="Save"
                            rendered="#{personBean.editMode}" icon="ui-icon-check" />
</f:facet>

the bean is session scoped and has the following attributes:

@ManagedBean(name = "personBean")
@SessionScoped
public class ReportPerson {
private boolean editMode;
private boolean reportMode;

}

the bean contains these method that changes the values of the booleans:

public String editPerson() {
    System.err.println("Edit Missing Person");
    editMode = true;
    reportMode = false;
    return "ReportNewPerson";
}

the problem is that these values remains until the session expires and as a result the components renders incorrectly

4

1 回答 1

1

如果您使用的是会话范围的 bean,那么您应该在构造函数中初始化它们,例如

public ReportPerson(){
//let say you want to show report mode by default
editMode = false;
reportMode = true;
}

之后,创建两个方法,如

public void inEditMode(){
           editMode = true;
           reportMode = false;
}

public void inReportMode(){
           editMode = false;
           reportMode = true;
}


现在调用你的渲染组件并调用这些方法#{reportPerson.editMode}并从你的支持bean中获取bean 。你可以像这样从sessionmap中获取bean#{reportPerson.reportMode}inReportMode()inEditModesessionmap

ReportPerson rp = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("reportPerson");

从中,您可以获取当前 bean 并从中调用

rp.inEditMode();


与使用 session scope 一样,您必须通过逻辑更改它们,因为它们将在整个会话期间保持其状态。

于 2013-05-18T13:38:44.727 回答