File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Issue using h:commandLink with rendered attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Issue using h:commandLink with rendered attribute" Watch "Issue using h:commandLink with rendered attribute" New topic
Author

Issue using h:commandLink with rendered attribute

Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
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

Joined: Dec 17, 2010
Posts: 23
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

Joined: Jun 11, 2003
Posts: 70
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

Joined: Jan 19, 2006
Posts: 26
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.


Phil
SCJP, SCWCD, SCEA
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
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

Joined: Jun 11, 2003
Posts: 70


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

Joined: Jan 06, 2011
Posts: 2

Naveen,

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

Thanks
Victor Hugo M Machado
Greenhorn

Joined: Jan 11, 2011
Posts: 22

what is the scope of the your BackBean "pageBean1" ?

maybe this is a bug.. clean all .class and temp files..
Gustavo Belloni Metzner
Greenhorn

Joined: Jan 06, 2011
Posts: 2

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

Joined: Jan 11, 2011
Posts: 22

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

Joined: Jun 11, 2003
Posts: 70
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.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue using h:commandLink with rendered attribute
 
Similar Threads
how to use TreeSelectionListener in a tree ?
Model update problem
Not able to use action attribute in JSF graphicImage
richfaces:: richdatatable problem.
commandLink/Button image src via CSS