使用托管 bean 进行导航

Navigation using managed beans

我正在使用 JSF 2.2 开发一个简单的项目,但在某些页面之间导航时遇到了一些问题。在项目中,我有一个通用模板,所有视图都是该通用模板的模板客户端。

这是我遇到麻烦的观点:

<h:body>

    <ui:composition template="./LayoutGeneral.xhtml">

        <ui:define name="content">
            <p:commandButton value="Registrar Comunidad" action="#{comunidadBean.irRegisterView}"/>
        </ui:define>

    </ui:composition>

</h:body>

在 commandButton 的操作中,我从托管 bean 调用了一个方法(Thar 托管 bean 有我调用的其他方法来更改页面,它们工作正常,但这个方法不起作用):

(托管 Bean)

@ManagedBean
@SessionScoped
public class ComunidadBean {    
    private String idComunidad;
    private String idPresidente;
    private String calle;
    private int numero;
    private int nVecinos;

    @EJB
    private ComunidadDAO ejb;

    public String register(){
        if(ejb.realizaRegistro(this)){
            return "principalView";
        } else{
            FacesMessage fm = new FacesMessage ("No se pudo registrar");
            FacesContext.getCurrentInstance().addMessage("msg", fm);
            return null;
        }
    }

    public String irRegisterView(){
        return "registroCView";
    }

}

所以方法 "register" 工作正常并且页面发生变化但是方法 "irRegisterView" 没有导航到 "registroCView" 页面。

有人知道这是怎么回事吗?

谢谢!

我不能评论,所以我写这个作为答案。

  • 我看到你的豆子是 SessionScoped。您需要实施 Serializable 因为 SessionScoped bean 在一段时间后会钝化。
  • 你有 LayoutGeneral.xhtml 中的表格吗?否则,此代码将永远无法工作,因为 commandButton 需要在表单中。
  • 为什么将方法称为 属性?在 JSF 2.2 和 EL 2.3 中,您可以这样调用方法:#{comunidadBean.isRegisterView()}.
  • 点击按钮时是否抛出异常?如果是,请粘贴堆栈跟踪。