【问题标题】:commandbutton not executing action if other attributes are bind to managed bean如果其他属性绑定到托管 bean,则命令按钮不执行操作
【发布时间】:2011-08-31 08:20:26
【问题描述】:

我对 jsf 真的很陌生。 我需要根据登录的用户权限启用、禁用、渲染或不渲染页面上的某些按钮。该页面绑定到请求范围的托管 bean,我将 commandbutton 的渲染或禁用属性绑定到托管 bean 的“可见”属性,并将 actionlistener 绑定到托管bean 方法,该方法的唯一目的是导航到另一个页面。 如果我将上述属性之一绑定到 visibile 属性,则不会调用导航方法,并且会在禁用或未呈现按钮的情况下重新呈现同一页面。 更迫切的需求是“我如何禁用或隐藏按钮?”的答案。当然, 但是,由于我猜这个问题是由于 jsf 页面生命周期和 bean 范围造成的,所以我还想直接学习一些比周围可以找到的 hello world 更高级的教程。

以下是部分代码。 提前感谢您的任何帮助

xhtml 页面

    <ui:define name="content">      
    <p:layoutUnit position="center" header="Dettaglio Pratica" scrollable="true">
        <h:form id="formDettaglioPratica">
        <!-- i've used javascript to redirect -->
        <p:commandButton styleClass="commandButton" value="Modifica pratica" ajax="false"
             rendered="#{praticaCtrl.visible}" onclick="navigateToChange();return false;"/>
        <p:commandButton id="backButton" styleClass="commandButton" value="Torna alla lista" 
        onclick="navigateToHome();return false;"></p:commandButton>
        <!-- never redirect to page defined in method checkIn -->
         <p:commandButton id="checkinButton" styleClass="commandButton" value="Rilascia pratica" ajax="false" 
           actionListener="#{praticaCtrl.checkIn}" disabled="#{praticaCtrl.visible}"></p:commandButton>

            <!-- printing output values -->

        <h:inputHidden id="idPratica" value="#{praticaCtrl.idPratica}"></h:inputHidden>
        <h:inputHidden id="idBox" value="#{praticaCtrl.box}"></h:inputHidden>
        </h:form>
    </p:layoutUnit>
</ui:define>

托管 bean 的相关部分

    public boolean isVisible() {
    return isVisible;
}

public void setVisible(boolean isVisible) {
    this.isVisible = isVisible;
}

public void setUser(IUserProvider user) {
    try{
    if(pratica.getUtenteCheckOut() == null
    || (pratica.getUtenteCheckOut().getMatricola().equalsIgnoreCase(user.getUser().getMatricola()))     
    )
        setVisible(true);   
    else
        setVisible(false);

    this.user = user;
    }
    catch(Exception ex)
    {log.error(ex);}
}

public void checkIn(ActionEvent event)
{
    try{
    log.info(String.format("modifica della pratica %s", idPratica));
    ajaxRedirect(String.format("../Box/ListaRichieste.xhtml?box=%s",box));
    }
    catch(Exception ex)
    {
        log.error(ex.getMessage(),ex);
    }
}

这是ajaxRedirect方法,我们不使用导航规则,方法也不返回字符串

protected void ajaxRedirect(String url) throws IOException {
    FacesContext.getCurrentInstance().getExternalContext()
    .redirect(url);     
}

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    所描述行为的答案可以在:http://forum.primefaces.org/viewtopic.php?f=3&t=13269&p=39996 找到,它指的是关于 stackoverflow 上类似问题的 BalusC 答案:commandButton/commandLink/ajax action/listener method not invoked or input value not updated

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-02
      • 2016-11-26
      • 1970-01-01
      • 2012-12-02
      • 2017-06-08
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多