h:commandButton从第二次点击开始工作 [英] h:commandButton works from the second click

查看:200
本文介绍了h:commandButton从第二次点击开始工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

 < h:form> 
< h:messages errorClass =errorMessageinfoClass =infoMessage
warnClass =warnMessage>< / h:messages>
< h:outputText value =login:/>
< h:inputText id =usernamevalue =#{securityBean.username}/>
< h:outputText value =password:/>
< h:inputSecret id =passwordvalue =#{securityBean.password}/>
< h:commandButton value =loginaction =#{securityBean.login}/>
< / h:panelGroup>

< h:panelGroup id =logoutlayout =blockrendered =#{securityBean.authorized}>
< h:commandButton value =logoutaction =#{securityBean.logout}/>
< / h:panelGroup>
< / h:表格>

这里是backing bean方法

<$ p $ (); getReternalContext()。getRequest();返回一个新的对象。
尝试{
request.login(username,password);
} catch(ServletException e){
FacesContext.getCurrentInstance()。addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,An Error Occured:Login failed,null));
e.printStackTrace();
}
返回null;
}

public Object logout(){
HttpSession session =(HttpSession)FacesContext.getCurrentInstance()。getExternalContext()。getSession(false);
if(session!= null){
session.invalidate();
}
返回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 不能帮助我..
如何让它工作正确?

解决方案

我认为你有这个问题,因为你需要重定向(使用结果,而不是null)主页或注销后的东西。我有这个问题,我解决了这样的问题:

pre $ public String logout(){
HttpSession session =(HttpSession )FacesContext.getCurrentInstance()。getExternalContext()。getSession(false);
if(session!= null){
session.invalidate();
}
返回redirectToHomePageAfterLogout;
}

希望这有助于您! :)

Here is my code:

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

here is backing bean methods

    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();
    }

After login, logout button appears, but it works only on the second click. As you can see no ajax is used here, so this http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichContainsAnotherForm can't help me.. How can I make it work correct? Thanks in advance.

解决方案

I think you have that problem because you need a redirection (using an outcome, not null) to a home page or something after logout. I had that problem, and I solved like this:

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

Hope this helps! :)

这篇关于h:commandButton从第二次点击开始工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆