【问题标题】:h:commandButton works from the second clickh:commandButton 从第二次点击开始工作
【发布时间】:2012-07-23 18:38:44
【问题描述】:

这是我的代码:

<h:form>
    <h:messages errorClass="errorMessage" infoClass="infoMessage"
                warnClass="warnMessage"></h:messages>
    <h:panelGroup id="login" layout="block" rendered="#{!securityBean.authorized}">
        <h:outputText value="login:"/>
        <h:inputText id="username" value="#{securityBean.username}"/>
        <h:outputText value="password:"/>
        <h:inputSecret id="password" value="#{securityBean.password}"/>
        <h:commandButton value="login" action="#{securityBean.login}"/>
    </h:panelGroup>

    <h:panelGroup id="logout" layout="block" rendered="#{securityBean.authorized}">
        <h:graphicImage id="img" library="img" name="login_success.jpg"/>
        <h:commandButton value="logout" action="#{securityBean.logout}"/>
    </h:panelGroup>
</h:form>

这里是支持 bean 的方法

    public Object login() throws ServletException {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        try {
            request.login(username, password);
        } catch (ServletException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An Error Occured: Login failed", null));
            e.printStackTrace();
        }
        return null;
    }

    public Object logout(){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if(session != null){
            session.invalidate();
        }
        return null;
    }
    public boolean isAuthorized() {
        return FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal() != null &&
                FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName() != null &&
                !FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName().isEmpty();
    }

登录后,会出现注销按钮,但仅在第二次单击时有效。正如你所看到的,这里没有使用 ajax,所以这个 http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichContainsAnotherForm 帮不了我.. 我怎样才能使它正常工作?提前致谢。

【问题讨论】:

  • 我使用 jsf 2..我发现一些主题中人们说这个问题在 jsf2 中已修复..但它不起作用..
  • 我们能看到调用这个表单的代码吗?
  • 您的 SecurityBean 是否在请求范围内?
  • 你的意思是这两个命令按钮的java操作方法代码吗?还是别的什么?
  • eljunior,是的,它是请求范围的..有问题吗?

标签: java forms jsf jsf-2


【解决方案1】:

我认为您遇到了这个问题,因为您需要在注销后重定向(使用结果,而不是 null)到主页或其他内容。我遇到了这个问题,我是这样解决的:

public String logout(){
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    if(session != null){
        session.invalidate();
    }
    return "redirectToHomePageAfterLogout";
}

希望这会有所帮助! :)

【讨论】:

  • 我今天晚些时候也试试。谢谢!
  • 不客气!如果其他人有同样的问题,您也可以将此标记为问题的答案以帮助其他人! :)
【解决方案2】:

这是因为表单提交的响应 URL 与生成它的请求相同。它产生的效果是 URL 始终落后于您实际所在的位置。

您可以为您的网址添加 ?faces-redirect=true 后缀,以强制 JSF 重定向您而不是转发您。

here 给出了更好的概述

但是,您的 logOut 操作应该返回一个 String(),它给出了在 logOut 函数完成后要移动到的页面的名称。

【讨论】: