我试图找出如何f:ajax
使用JSF和支持托管bean 通过调用注销用户.我遇到的问题是我无法弄清楚为什么Ajax监听器的调用顺序和登录表单的重新登记.
下面是非常简化的代码.代码的基本思想是这样的
if (uid != null) {
// show log out
} else {
// show log in
}
Run Code Online (Sandbox Code Playgroud)
我显然不了解ajax监听器和表单重新渲染是如何完成的.
JSF页面
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<f:view>
<h:form id="loginForm">
<c:choose>
<c:when test="${userBean.uid != null}">
<span>Hi, #{userBean.uid}</span>
<h:commandButton value="Logout">
<f:ajax event="click" listener="#{userBean.logout}" render="loginForm"/>
</h:commandButton>
</c:when>
<c:otherwise>
<span>User name: </span>
<h:inputText value="#{userBean.uid}" id="uid" />
<h:commandButton value="Login" action="#{userBean.login}" />
</c:otherwise>
</c:choose>
</h:form>
</f:view>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
豆
package test.auth;
import java.io.Serializable;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
@Named(value="userBean")
@SessionScoped
public class UserBean
implements Serializable
{
private static final long serialVersionUID = -4292770567982457272L;
private String uid;
/** Creates a new instance of UserBean */
public UserBean() {
}
public String getUid()
{
return uid;
}
public void setUid(String uid)
{
this.uid = uid;
}
public String login () {
return "faces/index.xhtml";
}
public void logout (AjaxBehaviorEvent event) {
this.uid = null;
}
}
Run Code Online (Sandbox Code Playgroud)
代码的问题是,当单击logout
表单重新加载但它仍处于登录状态时,即使uid
已设置为null
.我用调试器检查过这个.那么如何在ajax监听器之后执行渲染呢?
JSTL标记在视图构建期间执行,而不是在视图渲染时间期间执行.目前,JSF正在重新使用视图状态进行重新渲染,JSTL标记将不会被重新执行,因为它们不再存在.
您想要使用JSF rendered
属性.
<h:form id="loginForm">
<h:panelGroup rendered="#{userBean.uid != null}">
<span>Hi, #{userBean.uid}</span>
<h:commandButton value="Logout">
<f:ajax event="click" listener="#{userBean.logout}" render="loginForm"/>
</h:commandButton>
</h:panelGroup>
<h:panelGroup rendered="#{userBean.uid == null}">
<span>User name: </span>
<h:inputText value="#{userBean.uid}" id="uid" />
<h:commandButton value="Login" action="#{userBean.login}" />
</h:panelGroup>
</h:form>
Run Code Online (Sandbox Code Playgroud)