I’m trying to send an Ajax callback with RequestContext but I can’t get it work.
I tried the Primefaces login demo, but “args.loggedIn” in the javascript function is always undefined, whereas “args” is an Object.
Here is my code:
dialogLogin.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);
}
}
The javascript function “handleLoginRequest” is called, but “args.loggedIn” is always undefined. I can’t understand why…
Response sent (I get it from Firebug) is:
<?xml version="1.0" encoding="utf-8"?>
<partial-response>
<changes>
<update id="javax.faces.ViewState">
<![CDATA[3585116548358262356:-8608378680794066522]]>
</update>
</changes>
</partial-response>
Any help is greatly appreciated!
If the validation fails you get the following response from the application:
which contains a JSON response containing only
validationFailed : true. There is nologgedInfield in the response this is the reason why it isundefinedis you callargs.loggedIn.If the validation passes but the login fails you will get the following response:
this response has the
loggedInvalue so callingargs.loggedInresults infalse. Because there is novalidationFailedin the JSON response callingargs.validationFailedresults in undefined.