1

I am invoking a method from rendered attribute, where I noticed that the method is triggered multiple times in RENDER_RESPONSE phase.

It was also noticed that the method was trigered many times in other phases (APPLY_REQUEST_VALUES, PROCESS_VALIDATIONS etc.) also.

I saw a related query (Why is the getter called so many times by the rendered attribute?) where the reason behind these calls were told.

Is there a way where we can control this, so that the method is invoked only once.

My usage

<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;
    }
Community
  • 1
  • 1
Kaushal
  • 665
  • 2
  • 9
  • 21

1 Answers1

1

You cannot control the number of times the method is called, its framewroks lifecycle. You should find appropriate place to set the boolean value e.g. on command click action method, use this boolean value in render method so that logic is executed once in action method and only boolean is returned.

Other thing is you can protect the logic with if condition by probing for current lifecycle phase for render response.

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

But I'll prefer first option.

baba.kabira
  • 3,111
  • 2
  • 26
  • 37