简体   繁体   English

Ajax请求 - 在Spring Controller上调用不同的方法

[英]Ajax Request - Call different method on Spring Controller

I've been having a problem regarding using AJAX with Spring MVC. 关于在Spring MVC中使用AJAX我一直有问题。 I have a form which has a lot of fields, and each field retrieves data depending on the associated button that was clicked. 我有一个包含很多字段的表单,每个字段根据单击的相关按钮检索数据。

So, each one of my buttons needs to call an AJAX request. 因此,我的每个按钮都需要调用一个AJAX请求。 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? 我想知道在点击其他按钮后是否可以在我的Spring控制器中调用不同的方法?

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. 换句话说,我想对同一个控制器发出多个ajax请求,其中每个请求将在同一个控制器中调用不同的方法。

See this example : 看这个例子:

    // 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";  
    };*/  

Currently, I can have only one AJAX request. 目前,我只能有一个AJAX请求。 How can I modify this code so that I can have different functionality for different AJAX requests? 如何修改此代码,以便我可以为不同的AJAX请求提供不同的功能?

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. 首先,考虑当您从服务器检索数据而不修改该服务器的状态时,通常接受的标准是使用HTTP GET方法,而不是POST。 Thus, for your first two methods, you are misusing the HTTP Methods. 因此,对于前两种方法,您滥用HTTP方法。

Second, you can map individual URL patterns to a specific method using the value property of the RequestMapping annotation. 其次,您可以使用RequestMapping注释的value属性将各个URL模式映射到特定方法。

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: 第三,用于表示帐户详细信息资源的最RESTful方式是使用PathVariable注释并在实际路径中包含您的标识accountid:

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

 return somefunct.getAccountDetails(accountid);  

}  

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: 接下来,您可以使用不同的URL模式来表示您的帐户摘要,其中URL的构建方式类似于树,其中路径的前两部分再次是“帐户”和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);  

}  

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. 这只是一种奇特的方式,表示服务器的状态在此请求结束时会有所不同,并且对该资源的任何GET请求都将与更改之前的不同。 The appropriate HTTP method to use when modifying a resource or adding a resource to a collection is the HTTP POST Method. 在修改资源或向集合添加资源时使用的适当HTTP方法是HTTP POST方法。 When replacing a collection, the HTTP Method PUT is the generally accepted method of choice. 替换集合时,HTTP方法PUT是普遍接受的选择方法。

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. PUT和POST之间的另一个区别因素是PUT是幂等的,这意味着一遍又一遍地重复相同的请求不会改变服务器上的状态。 If hitting the same request multiple times creates more records, then use POST. 如果多次点击同一请求会创建更多记录,则使用POST。

Lastly, this request can be mapped to a URL as well. 最后,此请求也可以映射到URL。 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. 因此,我使用了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: 我还修改了你的参数列表以使用PathVariable从URL路径中获取accountid,并且我添加了一个RequestBody注释,以便您可以在请求的主体中发送一个对象,该对象可以被反序列化为Java对象:

/* 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";  
}

For more information on Spring MVC, please check out the Spring documentation on Spring MVC . 有关Spring MVC的更多信息,请查看Spring MVC上Spring文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM