Sven
Sven

Reputation: 6338

JSF Facelets rendering problem

I have a problem with my facelets:

I constricted a nav part that displays login information for about curren user and a logout button. Login works properly. But after a user logs out, the nav part of my page displays

Welcome, User (role)    [Logout_Button]

Whereas, what I want is the same thing that happens when you get to the login the first time:

Welcome, Guest

(Thank you Java drinker for those easy words :-))

Here you can see part of my template:

  <div id="metaContainer">
        <ui:include src="/metaMenu.xhtml" />
  </div>

That's my nav part with login info (called metaMenu.xhtml):

    <ui:composition xmlns="http://www.w3.org/1999/xhtml" lang="en"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
    <div id="login_info">
        <h:outputLabel value="Willkommen, "/>
        <h:outputLabel class="principal" value="#{metaNavData.principal}"/>
        <h:outputLabel value="#{metaNavData.role}"/>
    </div>
    <div id="meta_nav">
        <h:form name="loginform" rendered="#{authorisation.authenticated}">
        <h:commandLink action="#{loginController.logout}" rendered="#{authorisation.authenticated}">
        Logout</h:commandLink>
        </h:form>
    </div>
</ui:composition>

As BalusC and Java Drinker assumed it could be a java logic problem. I first need to say, that I use Apache Shiro for security issues. So here is the relevant Java code:

my loginController that contains the logout method:

@ManagedBean
@SessionScoped //Mistake!!! That should be RequestScoped, see below
public class LoginController {

private Subject currentUser;  // import org.apache.shiro.subject.*;

public LoginController() {
    currentUser = SecurityUtils.getSubject();
}

public String logout() {
    if (currentUser.isAuthenticated()) {
    currentUser.logout();
    }
    return "welcome.xhtml";
}

And her you can see my backed bean 'authorisation', which is supposed to provide information that can be used to hide comonents like the loginbutton:

@ManagedBean
@RequestScoped
public class Authorisation {

    private Subject currentUser;

    public Authorisation(){
        currentUser = SecurityUtils.getSubject();
    }

    public boolean getAuthenticated(){
        return currentUser.isAuthenticated();
    }

Upvotes: 0

Views: 1881

Answers (2)

Sven
Sven

Reputation: 6338

Thank's to BalusC and Java Drinker for your help, I have found my stupid mistake:

Are any of these beans session scoped perhaps

That was the wrong part! I used SessionScoped for my controller instead of RequestScoped. So here is the right controller:

@ManagedBean
@RequestScoped  // Here was my mistake
public class LoginController {

private Subject currentUser;  // import org.apache.shiro.subject.*;

public LoginController() {
    currentUser = SecurityUtils.getSubject();
}

public String logout() {
    if (currentUser.isAuthenticated()) {
    currentUser.logout();
    }
    return "welcome.xhtml";
}

I have found a very nice link about distinctions of managedBeans and how they should be scoped :-) KLICk

Perhaps this thread can give some inspiration for java beginner as I am too ;-)

Upvotes: 1

Java Drinker
Java Drinker

Reputation: 3167

Ok, so from what I understand, your problem is that even after a user logs out, the nav part of your page displays

Welcome, User (role)    [Logout_Button]

Whereas, what you want is the same thing that happens when you get to the login the first time:

Welcome, Guest

The problem, I think, is in your log out functionality. For whatever reason, even after you logout, #{authorisation.authenticated} is returning true. Secondly, #{loginController.principal} still contains the value of the previously logged in user, and his/her role. Are any of these beans session scoped perhaps, or caching the value somewhere?

And BalusC is right (as usual) in that you are using the word rendered here incorrectly. The button is the only element that is conditionally rendered. From what I see, the Welcome, User(role), vs Welcome, Guest is based solely on the value in the loginController.

If you can post some of your java code for those beans and login/logout functionality, we can try to help further

Upvotes: 2

Related Questions