2

I was initially trying to use Spring form tags and Validation on the Spring controller to submit form data. Since I have to update only the div in JSP page I don't think I can use this technique. So I am trying to use AJAX call. Following are the snippet of the code I have so far

Main JSP page (main.JSP)

<div id="header">...</div>
<div id="content">
  <div id="info1">....</info">
  <div id="info2">
    <%@ include file="/WEB-INF/jsp/myForm.jsp" %>
  </div>
  <div id="info3">...</div>
  ...
</div>
<div id="footer">...</div>

JSP Page (myForm.jsp)

<form:form action="/postFormData.htm" commandName="myEntryForm">
  //All inputs using <form:input> tags
</form:form>

Spring MVC Controller

@RequestMapping(value = "/telephone/sendMemberEntryEmail.htm", method = RequestMethod.POST)
public ModelAndView doSomeWork(Model model, @Valid MyEntryForm myEntryForm, BindingResult result) {
    if(result.hasErrors()) {
        return new ModelAndView("myForm", "myEntryForm", myEntryForm);
    }
    return new ModelAndView("successPage");
}

So here I am trying to send response (both with error or succcess) to div named info2. Issue I am facing is since I am using action it is updating whole main page with response.

Because of that I am thinking of doing AJAX call but I am not sure how I can collect form data and send to controller. I can't send each field as parameter as I have many fields and collection of related fields. I wan to send ti using MyEntryForm object.

Please suggest.

1
  • 1
    Try using form.serialize() and see. Your controller has to change as well to return data instead of MAV. Look at @ResponseBody annotation to return data. Commented Feb 10, 2015 at 20:39

1 Answer 1

1

Try this

          $("#myEntryForm").submit(function(e){
               $.ajax({
                    url: 'your form action url',
                    type: 'post',
                    data:$(this).serialize(),
                    success: function() {
                        alert('success');
                    }
                })        
           });

On your controller you should use @ResponseBody.

Sign up to request clarification or add additional context in comments.

2 Comments

yes i figure out that part. Now next question is how can i map 'result' from controller in JSp
You need to handle the response from the controller in the ajax success and process it. Here is the post which may answer your question, stackoverflow.com/questions/23267113/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.