aspose file tools*
The moose likes JSF and the fly likes <h:commandLink> rendered not updating on logout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "<h:commandLink> rendered not updating on logout" Watch "<h:commandLink> rendered not updating on logout" New topic
Author

<h:commandLink> rendered not updating on logout

Calvin Hughes
Greenhorn

Joined: Jul 24, 2011
Posts: 8

Hello,

I'm still new to JSF so I'm looking for any advice I can get. I have a problem updating my JSF components rendered attribute. Here is my code:

index.xhtml


MenuBean.java

faces-config.xml

I can login and logout just fine but the problem is that when I logout and get sent back to my index page, I can still see my logout button that shouldn't be rendered anymore. This goes away after clicking through another link so I thought that using redirect should work but to no avail.

Any tips will be much appreciated.
Thanks!
Calvin
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Well, for one thing, "rendered="#{menuBean.loginStatus()}"" is incorrect.

It should be "rendered="#{menuBean.loginStatus}"", where the menuBean should present a public method named "isLoginStatus()" that returns true when logged in and false when not logged in.


Customer surveys are for companies who didn't pay proper attention to begin with.
Calvin Hughes
Greenhorn

Joined: Jul 24, 2011
Posts: 8

Thank you Tim. I fixed the problem you mentioned but it doesn't seem to affect the problem I'm seeing. It is more like my page just isn't redirecting when it should be and I don't know why.

I would make it a regular <h:link> like all the rest of my buttons but for some reason JSF calls the outcome method of my link when the page is created so it automatically logs me out just from viewing the admin page.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

The difference between h:link and h:commandLink is that h:link does an immediate jump to the target, disregarding any updates made to the form. commandLink, on the other hand, validates the inputs, and if the data is valid, updates the backing bean, then invokes the action method. Which, by the way, should be declared as "action="#{menuBean.logout}"". Another case where you need to lose the parentheses.

The action method must be a public method with no parameters, and returning a String. The String is the navigation token, which originally was the label of a navigation rule, but in JSF2 can also be a direct reference to another JSF View. When it's empty or null, the invoking page is simply redisplayed.

The link tag was added for JSF2 as part of the support for bookmarkable JSF URLs. It should be used sparingly. The commandLink and commandButtons are more appropriate when doing data updates.

You can add the "redirect" element to a navigation rule and it will cause the displayed URL to match the destination. This costs extra, but is very useful/important in cases where the usual "URL lag" is not desirable, such as when navigating to a secured View.
Calvin Hughes
Greenhorn

Joined: Jul 24, 2011
Posts: 8

Hey Tim.

Thank you Thank you. Your comment lead me in the right direction to figuring out my problem.

It came down to <from-action> with <h:commandLink action=""> is not the same thing as using a <from-outcome> tag with <h:link outcome=""> in the faces-config.xml file.

I changed my method to not return a string that can be interpreted as a reference to a view and had to change my navigation rule back to using <from-outcome> in faces-config.xml

public String logout(){
FacesContext ctx = FacesContext.getCurrentInstance();
ExternalContext ectx = ctx.getExternalContext();
ectx.invalidateSession();
return "home";
}

<navigation-rule>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>

Thanks again!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: <h:commandLink> rendered not updating on logout