reset password
Author Message
dguhath
Posts: 61
Posted 09:41 Mar 15, 2015 |

Hello Dr. Sun,

I came across an interesting behavior in csns yesterday. I had a jsp page with a simple spring form. The get link of the page is of the form ...../mainpage?id=sometext. I was receiving id as a String. Now the jsp page renders perfectly but on the form post i was getting a 400 error. The only time it was working (and not throwing the 400 error) was when sometext was some integer (say /mainpage?id=123) and not some text (say /mainpage?id=abc). The solution to avoid this was rename the request parameter to anything other than id. Is id reserved or something?

Could you please help me understand this behavior.

Thanks.

hgadhia
Posts: 52
Posted 10:45 Mar 15, 2015 |

Did you have @RequestParam Integer id as one of your argument in the controller method? If you did, then the 400 error will come if you pass a string. Because string cannot be parsed to integer.

 

 

 

Last edited by hgadhia at 10:50 Mar 15, 2015.
cysun
Posts: 2935
Posted 11:31 Mar 15, 2015 |

My guess is that you bound some model attribute to the form, and that model attribute has an Integer or Long id field. When you submit the form, Spring Form will try to match and set the request parameters (both the ones in the form and the ones in the request query string) to the model attribute properties that have the same names, which is probably the cause of the problem. By the way, this could also cause a hard-to-debug error when the id request parameter is integer but is tended for some other purpose - you'll end up editing an unrelated object that happens to have that id.

dguhath
Posts: 61
Posted 11:58 Mar 15, 2015 |

Thanks Dr.Sun. That answers my question.

 

@HGADHIA - I know the mismatch issue that can arise but that's not the case here, the datatype are same (String). Thanks.