Reputation: 889
I am creating a user authentification in JSF2, and my header displayed on every page contains this:
<c:if test="#{user.loggedIn}">
<li><h:link value="Log out" outcome="#{user.logout}"/></li>
</c:if>
User refers to the userBean, and logout() simply invalidates the session, and issues a redirect to the login page. So when the user logs in, user.loggedIn becomes true, and logout link gets displayed, but it somehow immediately gets called, and the user is immediately logged out.
Does anyone have an idea why is this happening? I thought of using h:commandLink, but it requires a form, and I'm trying to avoid it.
Edit: I copied the wrong code... just my luck after spending an hour figuring out why the user cannot login. You can look at the previous revision to see miscopied code.
Upvotes: 0
Views: 1398
Reputation: 1108742
That can happen when the JSF tags are not recognized and parsed as such and it effectively get rendered as plain text wherein all EL expressions are evaluated as value expressions. It would in your case basically print the returned value of #{user.logout()}
. But while EL does that, the method's logic is of course invoked.
If you remove the action
attribute and open the page in browser and do a View Source, then you'll see an unparsed <h:commandLink>
tag instead of the generated HTML <a>
element.
Make sure that you have the h:
XML namespace definied on the right URI in the root tag of the view file.
xmlns:h="http://java.sun.com/jsf/html"
Update: the <h:link>
isn't intented to invoke bean actions. It's intented as a pure GET link. The outcome
is per specification evaluated as a value expression, not as a method expression. The outcome
must represent a view ID where the link has to navigate to. When the EL evaluates your method as a value expression, it's of course immediately invoked. Just keep using <h:commandLink>
in combination with a redirect. Additional advantage is that this isn't bookmarkable nor searchbot-crawlable.
Upvotes: 4
Reputation: 3634
This is the example from jsfToolbox:
<h:commandLink id="link1"
value="#{bundle.checkoutLabel}"
action="#{shoppingCartBean.checkout}" />
Get rid of your parens at the end of logout.
Upvotes: -1