【问题标题】:JSF- can't log outJSF-无法注销
【发布时间】:2014-02-07 17:06:05
【问题描述】:

这是我在index.xhtml中使用的模板:

<!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 &lt;username&gt;。应用内只有索引页和注册页

编辑: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>

【问题讨论】:

  • 请定义注销对我的一生都不起作用。您遇到的具体问题是什么:方法未执行,方法已执行但会话仍然存在?
  • @LuiggiMendoza:是的,抱歉:已编辑
  • 根据你的描述:你能检查你的注销方法的执行吗?添加一条日志消息以检查是否被调用(例如System.out.println("logging out...");
  • @LuiggiMendoza:未调用 - 嗯
  • 那是你的具体问题。请在您调用此注销方法的位置发布 JSF 代码。

标签: jsf session view-scope session-scope


【解决方案1】:

问题是您的&lt;h:commandButton&gt; 必须&lt;h:form&gt; 中才能工作。在这里查看:

<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。

【讨论】:

  • 面部护理。将用 CSS 度过余下的一天:D
猜你喜欢
  • 2011-12-17
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2011-11-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
相关资源
最近更新 更多