2014-02-07 5 views
1

을 로그 아웃 할 수없는 것은 내가 index.xhtml에서 사용하는 내 템플릿입니다. 등록 및 로그인 작업은 정상적으로 이루어 지지만 로그 아웃은 내 인생에서 효과가 없을 것입니다.JSF- 여기

@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

설명 : 로그 아웃 방법의 실행을 점검 할 수 있습니까? –

답변

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와 함께 즐겁게 :) –