Java Reference
In-Depth Information
<s:param name="contact" value="${contact}"/>
<img src="${contextPath}/images/update.png" border="0"/>
</s:link> |
For creating a new contact, the link is the same but without the contact
parameter.
Now that the form is in place, rub your hands together because we're
going to use a few neat tricks to submit the form via Ajax and handle
the response.
First, we'll use the onclick= event on the Save button to call submitForm ( ),
passing the button as a parameter:
Download email_36/web/WEB-INF/jsp/parts/contact_form.jsp
<s:submit name="save" onclick="return submitForm(this);"/>
In the JavaScript code, submitForm ( ) serializes the form and adds the
'_eventName' parameter with the name of the button:
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});
$.post(form.action, params, function (data) {
$('#contact_form').hide();
$('#contact_table').html(data);
});
return false ;
}
After creating the parameters, the form is posted via Ajax, and the
result, which is the updated contact table, is put back into the '#con-
tact_table' section, while the contact form, having done its task, is hid-
den again.
There's just one problem. . . can you figure out what it is?
What if the user submits the form with invalid input? As you know,
the default Stripes behavior in that case is to redisplay the form with
error messages. Since we're blindly putting the response into the con-
tact table portion of the page, the table gets clobbered by a form with
validation errors.
The problem is not that we're getting back the form instead of the
contact table; rather, it's that we need to put the form back into the
'#contact_form' portion instead of into '#contact_table' . In other words,
 
Search WWH ::




Custom Search