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 9059853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:04:38+00:00 2026-06-16T15:04:38+00:00

I have a method in my AccountController like @RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })

  • 0

I have a method in my AccountController like

 @RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })
    public String update(@ModelAttribute Account account) {
this.getAccountDao().save(account);
return "redirect:/users/account/";
}

I am using org.springframework.web.filter.HiddenHttpMethodFilter and so my view has a hidden form field as –

<form:form method="POST" modelAttribute="account">
      <input type="hidden" name="_method" value="PUT" />
....

Now my question is how does the controller know when to Create a new Account or to Update it or how does it know if the request is a POST or a PUT? To me it always looks like it is going to be PUT.

I just don’t like using anything apart from GET and POST. The controller does not need to care whether it needs to create a new one or update it. If the form has a hidden account id field, the service can figure out what DAO method to call.

EDIT
If this is only going to be a PUT request then I would need to create a new jsp for POST. Unfortunately both requests are very similar in that they need almost exact data to be submitted, except for account id. I want to be able to use this same method from the controller and the same jsp for both POST and PUT and depending on either the model – account is saved or updated.

  • 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-16T15:04:39+00:00Added an answer on June 16, 2026 at 3:04 pm

    The controller doesn’t know if you create or update an entity, it only knows the RequestMethods which the method reacts to.

    The hidden field you specified and the HiddenHttpMethodFilter you are using, result in PUT being the HTTP method, visible to your controller, as the filter changes the method in the request. (according to the javadocs).

    In result, the browser uses POST to transmit its data to the server, then the Filter runs and changes the method in the request to PUT, so for the application behind that Filter it looks like the request had been sent with PUT.

    I don’t see a problem in being very similar code, just factor the similar behavoir out to another method.. eg:

    @RequestMapping(method = { RequestMethod.POST})
    public String update(@ModelAttribute Account account) {
         // do POST specific things..
    
          // and common operations
          commonOperation();
    }
    
    @RequestMapping(method = { RequestMethod.PUT })
    public String updateWithPut(@ModelAttribute Account account) {
          // do PUT specific things...
    
          // and common operations
          commonOperation();
    }
    
    // code that put and post methods have in common
    private void commonOperation() {
     // a lot of common code
     // that needs to be done
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method like so private bool VerbMethod(string httpVerb, string methodName, string url,
In the AccountController, in the end of a method I have: RedirectToAction(EveryView, Account); In
I have this method which lets me translate the position of an object, animating
Can I override an ActionResult method. Say I have a method Index in AccountController
I have an Account Entity with a @ManyToMany relationship to Role Entity. (This way
I have a controller ( AccountController.cs ) whose final statement of its Index method
I have method that takes in a const vector reference. I want to put
I have a Login action method in my AccountController in asp.net MVC 3 application
I have a problem testing a logon method using existing AccountController (made by MVC)...
I have method overriding like below. Please ignore the way i written it may

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.