开发者

@RequestParam variables return a set of comma separated values, when a method gets called several times

开发者 https://www.devze.com 2023-04-06 06:38 出处:网络
I just noticed that a @RequestParam variable that I use to check POST params, such as usernames, or ids, behaves rather strangely when the controller method gets called consecutive times. Rather than

I just noticed that a @RequestParam variable that I use to check POST params, such as usernames, or ids, behaves rather strangely when the controller method gets called consecutive times. Rather than returning the current parameter value (for example, "Jason", "Michael", "John"), it returns a concatenated string of all parameter values that the method has be开发者_如何学Pythonen called with before that. It results in the variable having the value of: "Jason,Michael,John", rather than just "John" which was the last one.

I noticed that this strange behavior is per session. When I reduced the session duration to 1 minute only, i noticed that after the session is gone, so are the multiple values.

This thing never happens if I call request.getParameter("username"). Of course, I would like to stick to Spring MVC conventions if possible.

Is this a bug, or something intentional? How can I avoid it?


This is a bug in your JSP page. You likely have a hidden and an input with the same name. This results in a comma separated list of values.


Try without spring annotations:

Add to your method an attribute named HttpServletRequest

String s = request.getParameter("parameterName")


Your attribute might be saved internally by spring in the http session and re-used from there. Do you by any chance, on you spring controller class, have a configuration that would make that parameter session scoped (Either @SessionAttributes("username") on the class, or requireSession=true in your xml declaration of the controller bean)?

Or do you'add it to the model when you rediplay your page from the controller?


Faced the same issue while implementing an ajax login and found out that it was caused by the redirect that is triggered after the failing attempts to login. Somehow was accumulating the j_usernamen and j_password parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消