Java Reference
In-Depth Information
we need some way of knowing where to put the response fragment,
according to whether validation errors occurred.
My initial solution to this problem was to look for something in the
contact form fragment that isn't in the contact table—the <form> tag,
the error CSS class, or even an HTML comment that says, “There are
validation errors!” Although this works, it's an ugly and brittle solution.
Discussion with the friendly and talented Stripes community yielded a
more elegant solution: use a header in the HTTP response to indicate
that the form was successful. Upon receiving that header, the contact
table is refreshed as before; otherwise, the form with validation error
messages goes back into '#contact_form' .
OK, let's set a response header after successfully saving the contact. We
can do this in the save ( ) event handler, before returning the ForwardRes-
olution :
Download email_36/src/stripesbook/action/ContactFormActionBean.java
public Resolution save() {
// save the contact...
getContext().getResponse().setHeader("X-Stripes-Success","true");
return new ForwardResolution(ContactListActionBean. class ,
"table");
}
Adding the response header in contact_table.jsp also works:
<%= response.setHeader("X-Stripes-Success","true"); %>
I prefer adding the response in the action bean. I find that the intention
of signaling success is clearer in the save ( ) event handler method than
it is in the JSP.
On the client side, we need to look for the response header in the Ajax
callback function. Response headers are available in the standard XHR
( XMLHttpRequest ) object, which is the core object used when sending and
receiving data with Ajax. With jQuery, the XHR object is returned from
the function that sends the Ajax request— $.post , in our case. We can
look for the 'X-Stripes-Success' with a call to getResponseHeader ( ) on the
XHR object:
Download email_36/web/js/contact_form.js
function submitForm(button) {
var form = button.form;
var params = $(form).serializeArray();
params.push({name: '_eventName', value: button.name});
 
Search WWH ::




Custom Search