How do you handle Ajax requests in Spring MVC?

In Spring MVC (I work with 3.0.2), two HTTP methods: always (or, for the most part, it seems to me) are reserved (i.e. mapped to the corresponding handlers) that are GET and POST , for example

@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 called when a GET request is executed.


 @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 called when the POST request is executed. Assuming the Spring controller is indicated by the @RequestMapping(value="admin_side/Temp") annotation.


Now, what happens if I need to use Ajax and I need to perform various functions, than the previous methods do? I can not handle another method of using the GET method and the POST method, because it is already processed handlers (both HTTP method, GET and POST methods of treatment reserved for showForm() and onSubmit() , respectively).

For demonstration, I used the method=RequestMethod.PUT approach with Ajax , for example

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

and he worked as intended, but I felt that this was the best not . How do you handle Ajax requests in Spring MVC if you have such a scenario (in fact, it seems to me that this is a fairly common occurrence)? Should I (always) use RequestMethod.PUT ? (or is the best HTTP method for Ajax in Spring?)

Is there a way to map multiple methods to the same HTTP method in the same controller (the obvious answer should be no )? Again, what approach do you use when you need to work with Ajax in Spring MVC? I hope you understand what I mean! It is very difficult for me to express that my English is in its infancy.

+6
source share
3 answers

There is no such thing as a better method for AJAX.

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

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

+1
source

I think the real question is:

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

Who cares if you access it by making an AJAX request in the frontend? If the semantics of the call are different, specify a different URL. You can specify the URL pattern directly in the method, and not in the class, to avoid duplication of functions from this class.

+2
source

We can have several GET and POST methods in one controller, for this we need to use the value attribute

Example:

 @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; } 
+2
source

Source: https://habr.com/ru/post/922173/


All Articles