Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle Ajax requests in Spring MVC?

In Spring MVC (I'm working with 3.0.2), two HTTP methods are always (or mostly as it seems to me) reserved (i.e. mapped with appropriate handlers) which are GET and POST such as

@RequestMapping(method=RequestMethod.GET)
public String showForm(Map model)
{
     //Usually retrieve data from the database when the page is loaded.

     return "admin_side/Temp";
}

The above method is invoked when a GET request is made.


@RequestMapping(method=RequestMethod.POST)
public String onSubmit(@ModelAttribute("tempBean") @Valid TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response)
{
     //Perform some basic operations with the database like insert, update or delete when the form is submitted (by clicking a submit button or so).

     return "admin_side/Temp";
}

The above method is invoked when a POST request is made. Assuming that the Spring controller is designated with the @RequestMapping(value="admin_side/Temp") annotation.


Now, what happens, if I need to use Ajax and I need to perform different functionality than the preceding methods do? I can neither handle another method with the GET method nor with the POST method because there are already handlers mapped (both the HTTP methods, GET and POST are reserved to handle the showForm() and onSubmit() methods respectively).

For the sake of demonstration, I used the method=RequestMethod.PUT approach with Ajax such as

@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody String getStateList(@ModelAttribute("tempBean") @Valid TempBean tempBean, BindingResult error, HttpServletRequest request, HttpServletResponse response)
{
    return "Message";
}

and it worked as intended but I felt it should not be the best solution. How do you handle Ajax requests in Spring MVC, if you have such a scenario (actually, it seems to be quite usual to me)? Should I (always) use RequestMethod.PUT? (or what is the best HTTP method for Ajax in Spring?)

Is there a way to map more than one method with same HTTP method in the same controller (an obvious answer should be no)? Again, which approach do you use when you need to work with Ajax in Spring MVC? Hope you follow what I mean! It's quite difficult for me to express as my English is at the very initial stage.

like image 668
Tiny Avatar asked Aug 05 '12 18:08

Tiny


3 Answers

I think the real question is:

Why do you want the same URL/method combination to act different depending on how it is accessed?

Who cares if you are accessing it by making an AJAX request on the frontend? If the semantics of the call are different, give it a different URL. You can specify the URL pattern directly on the method, rather than on the class, to avoid having to duplicate functionality from that class.

like image 129
Thorn G Avatar answered Oct 23 '22 21:10

Thorn G


We can have multiple GET and POST methods in a single Controller, we need to use value attribute for this purpose

Ex:

@RequestMapping(method=RequestMethod.GET, value = "/showForm")
public ModelAndView showForm(){
}

@RequestMapping(method=RequestMethod.GET, value = "/processAjaxRequest")
public ModelAndView processAjax(){

 ModelAndView modelAndView = new ModelAndView("page.jsp");  
 modelAndView.addObject("ajax_response", ajax_response);
     return modelAndView;
}
like image 38
kartheek Avatar answered Oct 23 '22 21:10

kartheek


There's no such thing as best method for AJAX.

As for what methods you should use, it depends on the architectural style. The REST paradigm and its most common practical interpretation, ROA (Resource Oriented Architecture) make certain assumptions as to the semantics of HTTP methods. It's an increasingly popular approach and I think it's worth reading about. Making full use of the benefits of REST would probably require you to rethink your entire app design, though. If you decide to do it this way, read up on REST, ROA and JAX-RS, the Java standard for RESTful applications. Its implementations can be integrated with Spring.

Alternatively, you can just stick to GET and POST, as the most widely-supported methods. As for Spring itself, the reasonable way to do it would be to provide a separate bean (or perhaps a set of beans) to take care of your AJAX-based API. There will be no method "conflict" if you keep the URLs different.

like image 42
toniedzwiedz Avatar answered Oct 23 '22 19:10

toniedzwiedz