5 Replies Latest reply on Aug 5, 2012 5:03 PM by mcmurdosound

    rendrered <h:commandLink> problem

    lysy78

      Hello all,

       

      Configuration:

      • RichFaces 4.2.2
      • Mojarra 2.1.11
      • Jboss 6.1.0.Final

       

      I have a small problem with rendered command link. Let's take a look at this simple code:

       

      <h:head>

      </h:head>

      <h:body>

         

                    <h:form>

                                   <a4j:commandButton value="set flag to true" action="#{renderBean.action}" render="panelId"/>

                    </h:form>

         

                    <h:panelGroup id="panelId">

                                   <h5>rendered link</h5>

                                   <h:form rendered="#{renderBean.flag}">

                                                  <h:commandLink value="hard coded" action="/pages/renderTarget.xhtml" />

                </h:form>

                    </h:panelGroup>

         

                    <h5>normal links</h5>

                              <h:form>

                                             <h:commandLink value="hard coded" action="/pages/renderTarget.xhtml" />

                              </h:form>

         

      </h:body>

       

       

      Managed bean looks like this:

      @ManagedBean(name="renderBean")

      @ViewScoped

      public class RenderBean {

       

                private boolean flag;

       

                public boolean isFlag() {

                               return flag;

                }

       

                public void setFlag(boolean flag) {

                               this.flag = flag;

                }

       

                public void action(){

                               this.flag = true;

                }

       

      }

       

       

      Everything is quite simple. When we first see the page it looks like this:

       

       

      rf4.2.2render.jpg

       

      That's becouse renderBean.flag is set to false. So when we click button 'set flag to true' method action() i called and flag is set to true. Form is rendered and another link apears:

       

      rf4.2.2render2.jpg

       

      To this point everything looks great. But not so fast!

      When I click normal link it works, and new page apears. But when I click rendred link nothing happens. I'm not redirected to the target page. The question of course is why?

      I've been thinking for a while, but unfortunatelly without success.

       

      So please, could you help me?

       

      Hubert

        • 1. Re: rendrered <h:commandLink> problem
          healeyb
          1 of 1 people found this helpful
          • 2. Re: rendrered <h:commandLink> problem
            navurinv

            please try like

             

            <h:panelgrid  rendered="your boolean">  use this tag under form tag it will work

            1 of 1 people found this helpful
            • 3. Re: rendrered <h:commandLink> problem
              lysy78

              Hi guys,

               

              First of all, Breandan you are right. Thats the case. And a bit more as I will show you.

               

              So that what i've read in that article, and that what Navuri suggest (thanks for your reply) lead me to this approach:

               

              <h:form>

                   <a4j:commandButton value="set flag to true" action="#{renderBean.action}" render="renderedFormId"/>

              </h:form>

               

              <h5>rendered links</h5>

               

               

              <h:form id="renderedFormId" >

                   <h:panelGroup id="panelId" rendered="#{renderBean.flag}">

                       

                        <h:commandLink value="hard coded" action="/pages/renderTarget.xhtml" />

                                         

                   </h:panelGroup>

              </h:form>

               

               

               

               

              But you know what? That does not work either. More precisely, when bean is @ViewScoped it will never work, when bean is @SessionScoped it will work from the second click.

               

              But I tried something else. I replaced a4j:commandButton with h:commandButton and f:ajax elements. Look at that:

               

              <h:form>

                   <h:commandButton value="set flag to true" action="#{renderBean.action}">

                        <f:ajax render=":renderedFormId"/>

                   </h:commandButton>

              </h:form>

                 

              <h5>rendered links</h5>

                 

              <h:form id="renderedFormId" >

                   <h:panelGroup id="panelId" rendered="#{renderBean.flag}">

               

                        <h:commandLink value="hard coded" action="/pages/renderTarget.xhtml" />

                                          

                   </h:panelGroup>

              </h:form>

               

               

              That finally works as expected.

               

              So what are my final thoughts about that?

              Going backward to the original code snippet, there are two problems with that code. One is caused by JSF implementation itself, and the second, what is even more important for this community:

              it looks like a4j:commandButton does not work as expected. IMHO someone from Rich Faces team should take a look at that.

               

              Thanks again for your help guys,

              take care

               

              Hubert

              • 4. Re: rendrered <h:commandLink> problem
                healeyb

                Glad things are looking better, Navuri suggested using a panelGrid, although that would render a <table> which probably

                isn't what you'd require in this scenario. It's interesting that there's a difference between a4j:commandButton and

                h:commandButton you may want to log an issue.

                 

                The main point I'd make is that personally I don't see a great deal of reason to adopt a multiple form approach in an

                ajax environment except in rare circumstances (i.e. file upload and maybe dialogs).

                 

                All that ever seems to happen is that things stop working and people get in a muddle over naming containers and such

                like. The big win with multiple forms is limiting what gets submitted in the POST request body, but if you're doing

                ajax partial submits you're achieving this anyway using execute=. Remember that a4j:commandLink/Button default

                value for execute= is "@form" which differs from the f:ajax behavior of "@this", and you never know this could even

                explain the difference in behavior you're seeing (I'd not put any money on it but it's easily tested).

                 

                Regards,

                Brendan,.

                • 5. Re: rendrered <h:commandLink> problem
                  mcmurdosound

                  hmmm why are your rerendering a h:form in the first place?

                   

                  <h:form>

                       <h:commandButton value="set flag to true" action="#{renderBean.action}">

                            <f:ajax render=":renderedFormId:panelId"/>

                       </h:commandButton>

                  </h:form>

                     

                  <h5>rendered links</h5>

                     

                  <h:form id="renderedFormId" >

                       <h:panelGroup id="panelId" >

                   

                            <h:commandLink value="hard coded" rendered="#{renderBean.flag}" action="/pages/renderTarget.xhtml" />

                                              

                       </h:panelGroup>

                  </h:form>

                   

                  here you could simply rerender the h:panelGroup. Maybe this approach is more reliable.