Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:15:30+00:00 2026-06-03T17:15:30+00:00

I’ve been having a problem regarding using AJAX with Spring MVC. I have a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:15:32+00:00Added an answer on June 3, 2026 at 5:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.