6

If I submit this form:

<form id="confirmForm" method="POST">
    <input type="hidden" name="guid" value="guidval"/>
</form>

to this url:

/AltRT?guid=guidval

mapped to this controller method:

@RequestMapping(method = RequestMethod.POST)    
public String indexPost(@RequestParam String guid)

I am getting both values for my guid. So the value of guid is guidval,guidval. I would like to only get the value from the form.

Is there any way tell Spring to ignore query string parameters?

EDIT for more clarification: The query string is left over from another (get) request. So, if I could clear the query string that would work as well. Also, I do not want edit the name of the form input because I want this post endpoint to be available to other services without having to change them as well.

jlars62
  • 7,183
  • 7
  • 39
  • 60
  • 1
    Why are you using a query string, if your intent is to submit it in your hidden field? – Kalenda May 22 '15 at 17:48
  • Why don't you name the form input to something else, like guidVal? – j will May 22 '15 at 17:52
  • @QuoVadis and j will See edit – jlars62 May 22 '15 at 17:54
  • It is better to take it out of the URL querystring. But [this answer](http://stackoverflow.com/questions/19468572/spring-mvc-why-not-able-to-use-requestbody-and-requestparam-together) (example 5) to a very similar question suggests using both `RequestBody` and `RequestParam` - in that order, as such: `public String indexPost(@RequestBody String guid, @RequestParam String guid2)` – Vic May 22 '15 at 17:56
  • @Vic I would prefer the post method to not be dependent on the parameter existing in the query string – jlars62 May 22 '15 at 18:42

1 Answers1

1

You cannot do so because the query string will be sent in the HTTP message body of a POST request, http://www.w3schools.com/tags/ref_httpmethods.asp

There are two ways I could think of now

  1. set the form attribute action

    <form id="confirmForm" method="POST" action="AltRT">
        <input type="hidden" name="guid" value="guidval" />
    </form>
    
  2. convert the form data into JSON object to send it over and then catch it with @RequestBody in Spring if you have to use the original URL.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48