Loading popup with ajax
I have a jsp page that needs to load a popup using ajax. The content of the page is determined by the form filled in by the user.
Something like that:
JavaScript:
ajax('getPage.action', 'content_id', 'form_id');
foo.jsp:
<div id="content_id"></div>
<form id="form_id">
...
</form>
Java / spring:
@RequestMapping("getPage.action")
MyController extends AbstractCommandController {
RealDto dto = (RealDto)command;
...
return new ModelAndView("foo", data);
}
The hardest part for me is how easy it is to submit the form data as an ajax call. Can I use jQuery here? The form changes dynamically, so it would be enough to bother the list of all form fields.
Would it help to use Springs XT (which I never had)?
a source to share
Yes, you can use serialize to trivially transform the form to submit data.
$("#form1").submit(function() {
$.get("/desiredURL", $("#form1").serialize(), function(response) {
// send response data to a popup
}
}
You can use get or post to send data.
For the popup, I like facebox , but there are tons of options.
a source to share
jQuery form plugin helps you to easily convert a regular form to Ajax. You only need one line of code:
$("#myform").ajaxForm(
{beforeSubmit: validate, success: showPopup} );
a source to share
I don't know about jQuery, but for a prototype it's easy:
new Ajax.Request('getPage.action', {
parameters: $('form_id').serialize(true),
onSuccess: someMethod
);
Check out the Prototype API Docs .
This page has the same information for jQuery: http://docs.jquery.com/Ajax
a source to share