【问题标题】:Method is invoked multiple times if called from rendered attribute如果从渲染属性调用方法会被多次调用
【发布时间】:2013-02-26 01:42:23
【问题描述】:

我正在从渲染属性调用一个方法,我注意到该方法在 RENDER_RESPONSE 阶段被多次触发。

还注意到该方法在其他阶段(APPLY_REQUEST_VALUES、PROCESS_VALIDATIONS 等)也被多次触发。

我看到了一个相关查询 (Why is the getter called so many times by the rendered attribute?),其中告知了这些调用背后的原因。

有没有办法可以控制它,使该方法只被调用一次。

我的用法

<rich:panelMenuItem label="Menu1" actionListener="#{testMenuMB.panelMenuClickedAjax}" rendered="#{testMenuMB.checkForRendering('RoleA,RoleB')}"></rich:panelMenuItem>

public boolean checkForRendering(String rolesString){
System.out.println("Roles-->"+rolesString+FacesContext.getCurrentInstance().getCurrentPhaseId());
        boolean authorized = false;
        String [] rolesArray = rolesString.split(",");
        for (String string : rolesArray) {
            if(string!=null && accesibleRolesMap.containsKey(string)){
                authorized = true;
                break;
            }
        }
        return authorized;
    }

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    你无法控制方法被调用的次数,它的框架生命周期。您应该找到适当的位置来设置布尔值,例如在命令点击动作方法上,在渲染方法中使用这个布尔值,这样逻辑在动作方法中执行一次,只返回布尔值。

    另一件事是,您可以通过探测当前生命周期阶段的渲染响应来使用 if 条件保护逻辑。

     if(FacesContext.getCurrentInstance().getRenderResponse()){
        //logic
     }
    

    但我更喜欢第一个选项。

    【讨论】:

      猜你喜欢
      • 2020-07-16
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多