2014-02-07 89 views
1

這是我的我index.xhtml使用模板:JSF-不能註銷

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head> 
    <title><ui:insert name="title">Default title</ui:insert></title> 
    <h:outputStylesheet library="default" name="css/style.css" /> 
</h:head> 
<h:body> 
    <div id="wrapper" style="width: 800px; align: center"> 
     <div id="header"> 
      <div style="float: left"> 
       <h:graphicImage library="default" 
        name="img/032-ivan-the-terrible-theredlist.jpg" /> 
      </div> 
      <h:form id="loginForm" rendered="#{sessionScope.user == null}"> 
        Username: <h:inputText id="username" 
        value="#{userController.user.username}" /> 
        Password: <h:inputSecret id="password" 
        value="#{userController.user.password}" /> 
       <h:commandButton value="Login" action="#{userController.login}" /> 
      </h:form> 
      <h:commandButton value="Logout" action="#{userController.logout}" 
       rendered="#{sessionScope.user != null}" /> 
      <h:outputLink target="register.xhtml" 
       rendered="#{sessionScope.user == null}">Register</h:outputLink> 
      <h:outputText rendered="#{sessionScope.user != null}" 
       value="You are logged in as #{sessionScope.user.username}" /> 
     </div> 
     <div id="menu" style="clear: both">Menu</div> 
     <div id="content"> 
      <ui:insert name="content">Default content</ui:insert> 
     </div> 
     <div id="footer">Footer</div> 
    </div> 
</h:body> 
</html> 

這裏是用戶控制器。註冊並登錄工作正常 - 但註銷將不適用於我的生活。

@ManagedBean 
@ViewScoped 
public class UserController { 

    // http://stackoverflow.com/a/10691832/281545 
    private User user; 
    @EJB // do not inject stateful beans ! 
    private UserService service; 

    public User getUser() { 
     return user; 
    } 

    @PostConstruct 
    void init() { 
     // http://stackoverflow.com/questions/3406555/why-use-postconstruct 
     user = new User(); 
    } 

    public String login() { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     try { 
      System.out.println("Pass :" + user.getPassword()); 
      user = service.find(user.getUsername(), user.getPassword()); 
      context.getExternalContext().getSessionMap().put("user", user); 
      return "/index.xhtml?faces-redirect=true"; 
     } catch (NoResultException e) { 
      context.addMessage(null, new FacesMessage(
       FacesMessage.SEVERITY_ERROR, "Unknown login, please try again", 
       null)); 
      return null; 
     } 
    } 

    public String register() { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     user = service.register(user); 
     if (user.getIduser() == 0) { 
      context.addMessage(null, new FacesMessage(
       FacesMessage.SEVERITY_ERROR, "Registration failed", null)); 
      return null; 
     } 
     context.getExternalContext().getSessionMap().put("user", user); 
     return "/index.xhtml?faces-redirect=true"; 
    } 

    public String logout() { 
     FacesContext.getCurrentInstance().getExternalContext() 
      .invalidateSession(); 
     return "/index.xhtml?faces-redirect=true"; 
    } 
} 

所以當登錄按鈕重定向我到索引並顯示我的登錄名和註冊送我到索引登錄的新註冊用戶,當我打的退出按鈕只剩下我盯着You are logged in as <username>。只有索引頁面和註冊頁面中應用

編輯:index.xhtml

<ui:composition template="/WEB-INF/xhtml/template.xhtml" 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <ui:define name="title">All Times Classics - Welcome</ui:define> 
    <ui:define name="content"> 
     <h1 style="text-align: center">Welcome to All Times Classics</h1> 
     You can <a href="register.xhtml">Register</a> if you want to enjoy all 
      the goodies we have to offer. 
    </ui:define> 
</ui:composition> 
+0

請定義*註銷不會爲我的生活工作*。你有什麼具體問題:方法沒有被執行,方法被執行,但會話仍然活着? –

+0

@LuiggiMendoza:對不起:編輯 –

+0

通過您的描述:你能夠檢查你的註銷方法的執行?添加日誌消息來檢查是否被調用(例如'System.out.println(「註銷...」);'。 –

回答

2

的問題是,你的<h:commandButton>必須一個<h:form>內才能工作。點擊此處查看:

<h:form id="loginForm" rendered="#{sessionScope.user == null}"> 
    Username: <h:inputText id="username" value="#{userController.user.username}" /> 
    Password: <h:inputSecret id="password" value="#{userController.user.password}" /> 
    <h:commandButton value="Login" action="#{userController.login}" /> 
</h:form> 
<!-- needs to be enclosed by <h:form> --> 
<h:form> 
<h:commandButton value="Logout" action="#{userController.logout}" 
    rendered="#{sessionScope.user != null}" /> 
</h:form> 

您可以檢查這裏的解釋:commandButton/commandLink/ajax action/listener method not invoked or input value not updated,原因1.

+1

Facepalm。將花費剩下的與CSS的一天:D –

+1

不客氣。玩得開心用CSS :) –