我正在尝试使用 RequestContext 发送 Ajax 回调,但无法正常工作。
我尝试了Primefaces 登录演示,但 javascript 函数中的“args.loggedIn”始终未定义,而“args”是一个对象。
这是我的代码:
对话框登录.xhtml:
<h:body>
<h:outputLink
id="loginLink"
value="javascript:void(0)"
onclick="dlg.show()"
title="login">
Click
</h:outputLink>
<p:dialog id="dialog" header="Login" widgetVar="dlg">
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{logBean.username}"
id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{logBean.password}"
id="password" required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login" update=""
actionListener="#{logBean.login}"
oncomplete="handleLoginRequest(xhr, status, args)"/>
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
<script type="text/javascript">
function handleLoginRequest(xhr, status, args) {
if(args.validationFailed || !args.loggedIn) {
jQuery('#dialog').effect("shake", { times:3 }, 100);
} else {
dlg.hide();
jQuery('#loginLink').fadeOut();
}
}
</script>
</h:body>
LogBean.java:
@ManagedBean
public class LogBean {
private String username;
private String password;
// getters & setters
public void login(ActionEvent actionEvent) {
RequestContext context = RequestContext.getCurrentInstance();
boolean loggedIn = false;
if(username != null && username.equals("admin") && password != null && password.equals("admin")) {
loggedIn = true;
} else {
loggedIn = false;
}
System.out.println("loggedIn : " + loggedIn);
context.addCallbackParam("loggedIn", loggedIn);
}
}
调用了 javascript 函数“handleLoginRequest”,但“args.loggedIn”始终未定义。我不明白为什么...
发送的响应(我从 Firebug 得到)是:
<?xml version="1.0" encoding="utf-8"?>
<partial-response>
<changes>
<update id="javax.faces.ViewState">
<![CDATA[3585116548358262356:-8608378680794066522]]>
</update>
</changes>
</partial-response>
任何帮助是极大的赞赏!