Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Request - Call different method on Spring Controller

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.

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?

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.

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. How can I modify this code so that I can have different functionality for different AJAX requests?

like image 389
Jc dev Avatar asked May 12 '12 04:05

Jc dev


1 Answers

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.

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

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

}  

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

}  

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.

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.

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

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

like image 93
jmort253 Avatar answered Oct 21 '22 18:10

jmort253