This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue using h:commandLink with rendered attribute

 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use the rendered attribute, the action listener method does NOT get invoked :



However when I remove the rendered attribute, the action listner DOES get invoked:



Any ideas why this might happen ?


Note:- rendered="#{pageBean1.hasSavePrivilege}'' itself works properly. i.e when value is false link does not get rendered and vice versa.



 
John Tannel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen,

Does the action listener NEVER get invoked or only when the control does not get rendered? I believe if it does not get rendered, then the listener should not be invoked. I'm not quite sure as to behavior when it is rendered. I would assume it should get invoked.

Also, can you show your code for your action listener? It may help more with debugging.

John
 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When rendered="true" the link is displayed as expected. However when clicking link, the target action listener is not called.

The debug statments from my phase listener shows that all phases are executed:



I'm not sure what's wrong in the page. Has anyone run into this problem ?

I have a simple debug statment in target listener method.

 
Phil Haigh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen Sampra wrote:When I use the rendered attribute, the action listener method does NOT get invoked :

Any ideas why this might happen ?


Remember that because you are invoking an action, the JSF lifecycle must execute first. Therefore the view is reconstructed, validations fire, values injected etc... it sounds to me like your commandLink is not being rendered because pageBean1.hasSavePrivilege evaluates to something other than true after the view has been reconstructed.

You can prove this one way or the other by hardcoding the pageBean1.hasSavePrivilege method to always return true and leaving the rendered property 'as is' rather than removing it. If clicking on the link causes the action to be invoked, then this is definitely your problem. A simple fix is to add a hidden field to the page and a set method to your bean, so that the previously calculated value of hasSavePrivilege is restored:

and:

Now when you click the link the property will be restored by JSF for you, rendered will evaluate to true and the action will be invoked.

Hope that helped.
 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil,

You are on the right track. Part of your explanation fits what I found in my debugging. By the way hasSavePrivilege is passed in the initial request to my page, so I have the <h:inputHidden ../> mechanism already setup to save this value on page to be used in subsequent form submissions/page rendering.

Here's the issue:
hasSavePrivilege is true. I have verified that. However on clicking "Save" link, the bean field gets updated to true only at Update Model phase(my bean has REQUEST scope). At this point my target action DOES NOT get invoked at Invoke Application phase(Not sure why?).

When I forcefully initialize hasSavePrivilege value to true in bean constructor(NOTE:- During Apply Request values phase and before update model), the target action gets invoked!

I tried using immediate="true" in hidden field to force the bean update at Apply Request values phase, but that does not work.
I'm not sure why <h:commandLink> is behaving this way with rendered attribute.

So looks like only solution is to to retrieve hidden field value from HttpRequest in bean constructor and set it. This will make sure target action gets invoked if hasSavePrivilege=true.

Any other ideas?



 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hasSavePrivilege passed to JSF page on initial request is read in bean constructor and set as bean property. Bean has request scope.
A hidden field on page is used to preserve hasSavePrivilege. The initialization of hasSavePrivilege property happens through the setter then afterwards, using hidden field value on page. i.e. initialization NOT in the bean constructor.

The problem is even when hidden field hasSavePrivilege=true the action method is not invoked.

Any help appreciated.
 
Gustavo Belloni Metzner
Greenhorn
Posts: 2
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen,

I have the same problem, did you find any solution?

Thanks
 
Victor Hugo M Machado
Greenhorn
Posts: 22
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the scope of the your BackBean "pageBean1" ?

maybe this is a bug.. clean all .class and temp files..
 
Gustavo Belloni Metzner
Greenhorn
Posts: 2
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor,

In my tests if I use request scope doesn't work, but if I use session scope works fine. I don't see it as a good solution... any idea?
 
Victor Hugo M Machado
Greenhorn
Posts: 22
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something happens in your Life Cycle that prevents operation of the button ..
debug all classes at this process envolved .. not business process.. only the JSF LyfeCycle classes

I solved very similar problems ...
on time I had a popup process and that throws a <f:message> for the first page and the message never was show.. then I solved with a Post/Redirect/Get (PRG) design pattern..
take a look
are you brazilian? If yes here too
 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gustavo,

My solution was to use <a4j:keepAlive />
I put these hidden params in separate bean and inject it to my backing bean. Then on facelet page I preserve the new bean using <a4j:keepAlive beanName="hiddenParamBean" />. All beans are in request scope.

In backing bean constructor I initialize using values from hiddenParamBean.

There is definietly an issue with <h:commandLink> and using EL for the rendered="#{}" attribute value.

As you might have also observed there is no disabled attribute for <h:commandLink> unlike other components.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic