如何在Spring MVC中处理Ajax请求?

时间:2022-11-12 23:22:01

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

在Spring MVC(我使用的是3.0.2)中,两个HTTP方法总是(或者在我看来大部分都是)保留(即映射到适当的处理程序),它们是GET和POST

@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.

在发出GET请求时调用上述方法。


@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.

在发出POST请求时调用上述方法。假设Spring控制器被指定为@RequestMapping(value="admin_side/Temp")注释。


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).

现在,如果我需要使用Ajax,而我需要执行与前面方法不同的功能,会发生什么呢?我既不能用GET方法处理另一个方法,也不能用POST方法处理另一个方法,因为已经有映射的处理程序(HTTP方法、GET和POST都被保留来分别处理showForm()和onSubmit()方法)。

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

为了演示,我使用了方法=RequestMethod。使用Ajax进行处理

@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?)

我觉得它不应该是最好的解决方案。如果您有这样的场景(实际上,对我来说似乎很常见),如何在Spring MVC中处理Ajax请求?我应该(总是)使用RequestMethod.PUT吗?(或者在Spring中,Ajax最好的HTTP方法是什么?)

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.

是否有一种方法可以在同一控制器中使用相同的HTTP方法映射多个方法(一个明显的答案应该是no)?同样,当您需要在Spring MVC中使用Ajax时,您会使用哪种方法呢?希望你理解我的意思!因为我的英语才刚刚开始,所以很难表达。

3 个解决方案

#1


1  

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

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.

至于应该使用什么方法,这取决于架构风格。REST范式及其最常见的实践解释ROA(面向资源的体系结构)对HTTP方法的语义做了一些假设。这是一种越来越流行的方法,我认为值得一读。充分利用REST的好处可能需要你重新考虑整个应用设计。如果您决定这样做,请阅读REST、ROA和JAX-RS,这是RESTful应用的Java标准。它的实现可以与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.

或者,您可以坚持使用GET和POST作为最受广泛支持的方法。至于Spring本身,合理的做法是提供一个独立的bean(或者一组bean)来处理基于ajax的API。如果url不同,就不会有方法“冲突”。

#2


2  

I think the real question is:

我认为真正的问题是:

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

为什么要使用相同的URL/方法组合来根据访问的方式进行不同的操作?

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.

谁会关心您是否通过在前端发出AJAX请求来访问它?如果调用的语义不同,则给它一个不同的URL。您可以直接在方法上指定URL模式,而不是在类上指定,以避免重复该类的功能。

#3


2  

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

我们可以在一个控制器中有多个GET和POST方法,为此需要使用value属性

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;
}

#1


1  

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

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.

至于应该使用什么方法,这取决于架构风格。REST范式及其最常见的实践解释ROA(面向资源的体系结构)对HTTP方法的语义做了一些假设。这是一种越来越流行的方法,我认为值得一读。充分利用REST的好处可能需要你重新考虑整个应用设计。如果您决定这样做,请阅读REST、ROA和JAX-RS,这是RESTful应用的Java标准。它的实现可以与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.

或者,您可以坚持使用GET和POST作为最受广泛支持的方法。至于Spring本身,合理的做法是提供一个独立的bean(或者一组bean)来处理基于ajax的API。如果url不同,就不会有方法“冲突”。

#2


2  

I think the real question is:

我认为真正的问题是:

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

为什么要使用相同的URL/方法组合来根据访问的方式进行不同的操作?

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.

谁会关心您是否通过在前端发出AJAX请求来访问它?如果调用的语义不同,则给它一个不同的URL。您可以直接在方法上指定URL模式,而不是在类上指定,以避免重复该类的功能。

#3


2  

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

我们可以在一个控制器中有多个GET和POST方法,为此需要使用value属性

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;
}