如果其他属性绑定到托管 bean,则命令按钮不会执行操作

发布于 2024-12-02 09:19:24 字数 2624 浏览 0 评论 0原文

我对jsf真的很陌生。 我需要根据登录的用户权限启用、禁用、渲染或不渲染页面上的某些按钮。该页面绑定到请求范围的托管 bean ,我将命令按钮的渲染或禁用属性绑定到托管 bean 的“可见”属性,并将动作侦听器绑定到托管 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);     
}

i'm really new to jsf.
I need to enable,disable, render or not render some buttons on a page depending on the logged user privileges. The page is bound to a request scoped managed bean , i bind the rendered or disabled attribute of the commandbutton to a "visible" property of the managed bean, and an actionlistener to a managedbean method which has the only purpose to navigate to another page.
If i bind one of the above properties to the visibile property, the navigation method doesn't get called, and the same page gets rerendered with the button disabled or not rendered.
The more urget need is the answer to "how do i disable or hide the button?" of course,
But , since i guess the problem is due to jsf page life cycle and the bean scope, i'd like also to be direct to some tutorial a bit more advanced than the hello world that can be found around.

below are part of the code.
Thank you in advance for any help

the xhtml page

    <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>

the relevant part of the managed 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);
    }
}

this is the ajaxRedirect method,we don't use the navigation rules neither methods return string

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-09 09:19:24

可以在以下位置找到所描述行为的答案: http ://forum.primefaces.org/viewtopic.php?f=3&t=13269&p=39996 ,它指的是BalusC 在 stackoverflow 上回答类似问题: commandButton/commandLink/ajax action/listener method not invoked或输入值未更新

the answer for the described behaviour can be found at : http://forum.primefaces.org/viewtopic.php?f=3&t=13269&p=39996 ,which refers a BalusC answer on a similar question on stackoverflow : commandButton/commandLink/ajax action/listener method not invoked or input value not updated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文