Ajax请求 - 调用不同的方法对春季控制器 [英] Ajax Request - Call different method on Spring Controller

查看:212
本文介绍了Ajax请求 - 调用不同的方法对春季控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有使用AJAX和Spring MVC有关的问题。我有具有很多领域的一种形式,并且根据不同的相关联的按钮被点击,每个字段中检索数据。

I've been having a problem regarding using AJAX with Spring MVC. I have a form which has a lot of fields, and each field retrieves data depending on the associated button that was clicked.

所以,我的按钮,每一个都需要调用一个AJAX请求。每个响应将显示在相应的领域。

So, each one of my buttons needs to call an AJAX request. Each response will be displayed on the associated field.

我不知道是否有可能调用不同的方法,我的春节,控制器,一旦我点击了不同的按钮?

I wonder if it is possible to call a different method in my Spring controller once I clicked on a different button?

换句话说,我想使多个Ajax请求到同一控制器,每个请求调用在同一个控制器不同的方法。

In other words, I want to make multiple ajax requests to the same controller where each request will call a different method in that same controller.

请参阅下面的例子:

    // when get account detail is clicked it will call this method  
@RequestMapping(method=RequestMethod.POST)  
    public @ResponseBody String getAccountDetails(@RequestParam(value="accountid") String accountid){  

     return somefunct.getAccountDetails(accountid);  

    }  



// when get account summary is clicked it will call this method  
@RequestMapping(method=RequestMethod.POST)  
    public @ResponseBody String getAccountSummary(@RequestParam(value="accountid") String accountid){  

      return somefunct.getAccountSummary(accountid);  

    }  



/* when submit button is clicked... Form is submitted for saving*/  
@RequestMapping(method=RequestMethod.POST)  
    public String submitForm(){  
        // save here  
        return "myform";  
    };*/  

目前,我只能有一个Ajax请求。我怎么能修改此code,这样我可以有不同的AJAX请求不同的功能?

Currently, I can have only one AJAX request. How can I modify this code so that I can have different functionality for different AJAX requests?

推荐答案

首先,考虑当你从服务器检索数据,而无需修改服务器的状态,普遍接受的标准是使用HTTP GET方法,而不是POST 。因此,对于你的前两种方法,你滥用HTTP方法。

First, consider that when you retrieve data from a server without modifying the state of that server, the commonly accepted standard is to use the HTTP GET method, not POST. Thus, for your first two methods, you are misusing the HTTP Methods.

其次,你可以使用RequestMapping注解的value属性单独的URL模式映射到一个特定的方法。

Second, you can map individual URL patterns to a specific method using the value property of the RequestMapping annotation.

三,最RESTful方式重新present的帐户信息资源是使用PathVariable注释和包括的实际路径的识别帐户ID:

Third, the most RESTful way to represent your account details resource is to use the PathVariable annotation and include your identifying accountid in the actual path:

@RequestMapping(value="/account/{accountid}/details", method = RequestMethod.GET)
public @ResponseBody String getAccountDetails(@PathVariable(value="accountid") String accountid){  

 return somefunct.getAccountDetails(accountid);  

}  

接下来,你可以重新present使用其中的URL是建立像一棵树,在路径的前两部分是再次帐户和帐户ID不同的URL模式您的帐户摘要:

Next, you can represent your account summary using a different URL pattern where the URL is built like a tree, where the first two parts of the path are once again "Account" and the accountid:

// when get account summary is clicked it will call this method  
@RequestMapping(value="/account/{accountid}/summary", method=RequestMethod.GET)  
public @ResponseBody String getAccountSummary(@PathVariable(value="accountid") String accountid){  

    return somefunct.getAccountSummary(accountid);  

}  

现在,您提交方法,在另一方面,有副作用。这是说,你的服务器的状态将在这个请求结束不同的只是一种奇特的方式,任何获得该资源提出的要求会有所不同比他们之前的变化。适当的HTTP方法来修改资源或将资源添加到一个集合是HTTP POST方法时使用。当更换一个集合,HTTP方法PUT是公认的首选方法。

Now, your submit method, on the other hand, has side effects. This is just a fancy way of saying that the state of your server will be different at the end of this request, and any GET requests made to that resource will be different than they were prior to the change. The appropriate HTTP method to use when modifying a resource or adding a resource to a collection is the HTTP POST Method. When replacing a collection, the HTTP Method PUT is the generally accepted method of choice.

PUT和POST之间的另一个差异化因素是,PUT是幂等的,也就是说,相同的请求重复了一遍又一遍不改变服务器上的状态。如果击中同一请求多次创造更多的记录,然后使用POST。

Another differentiating factor between PUT and POST is that PUT is idempotent, meaning that the same request repeated over and over again doesn't change the state on the server. If hitting the same request multiple times creates more records, then use POST.

最后,该请求可以被映射到一个URL为好。在下面的例子中,我假设你正在创建一个新帐户记录,并在数据库帐户的集合中插入新记录。因此,我用POST。我还修改了参数列表中使用PathVariable采取从URL路径的帐户ID,我增加了一个RequestBody注解,让您可以在请求的主体,它可以被反序列化到Java对象发送对象:

Lastly, this request can be mapped to a URL as well. In the example below, I've assumed you are creating a new Account record and inserting a new record in the collection of accounts in the database. Thus, I've used POST. I also modified your parameter list to use PathVariable to take the accountid from the URL path, and I added a RequestBody annotation so that you can send an object in the body of the request, which could be deserialized into a Java object:

/* when submit button is clicked... Form is submitted for saving*/  
@RequestMapping(value="/account/{accountid}", method=RequestMethod.POST)  
    public String submitForm(@PathVariable String accountid, @RequestBody Account account){  
        // save here  
        return "myform";  
}

有关Spring MVC的更多信息,请查看<一href="http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/mvc.html">Spring文件对Spring MVC的。

For more information on Spring MVC, please check out the Spring documentation on Spring MVC.

这篇关于Ajax请求 - 调用不同的方法对春季控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆