0

Hello i am working with JSF2 and i am having some trouble with the session. My problem is that i login and the user name appears like its logged but when i navigate to other page the name does not appear anymore. This started happening when i make the logout option. I have the login and logout methods in the same controller. And the text where its shows the name of the user and logout button is a facelet template, because i wanted it to be shown in all the pages that have that template.

this is the code for the login and the logout:

public String login(){
    try {
        user = ejb.Autenticar(this.mail, this.pass);

        if(user != null){
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            Map<String, Object> sessionMap = externalContext.getSessionMap();
            sessionMap.put("USER", user);

            return "LoginOK";
        }

    } catch (Exception e) {
        return null;
    }
    return null;
}

public String logout(){
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "LoginView";
}

This is the code of the facelet template (The user name and the logout button are in the divSessionUser):

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui">

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <h:outputStylesheet name="./css/default.css"/>
    <h:outputStylesheet name="./css/cssLayout.css"/>
    <title>Facelets Template</title>
</h:head>

<h:body>

    <div id="divSessionUser">
        <span>
            <b>Session user: </b>
            <p:outputLabel value="#{loginControlador.user.name}"/>
        </span>
        <p:button value="Logout" outcome="#{loginControlador.logout()}"/>
    </div>

    <div id="top" class="top">
        <h:graphicImage library="images" name="header1.png" styleClass="topImage"></h:graphicImage>
    </div>
    <div>
        <div id="left">
            <ul>
                <li> <h:link outcome="indexView" value="Home" /></li>
                <li> <h:link outcome="registroCView" value="Register Comunity" /></li>
                <li> <h:link outcome="LoginView" value="Login" /></li>
                <li> <h:link outcome="registroUserView" value="Register User" /></li>
            </ul>
        </div>
        <div id="content" class="left_content">
            <ui:insert name="content">Content</ui:insert>
        </div>
    </div>
</h:body>

Megan..
  • 25
  • 6

1 Answers1

0

I dont know if this is the correct way of doing this, but it got solved by inserting the div=divSessionUser of the facelet template inside a form and change the button for commandButton.

Hope it helps anyone, and if someone have a better answer please tell us!!! Thanks!

Megan..
  • 25
  • 6
  • I was going to recommend you change the p:button to a p:commandButton and change the outcome to action. If it worked out for you then, that is great! – ArnoldKem Apr 08 '16 at 07:10