@RequestParam variables return a set of comma sepa

2019-08-05 03:47发布

问题:

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 been 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?

回答1:

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.



回答2:

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?



回答3:

Try without spring annotations:

Add to your method an attribute named HttpServletRequest

String s = request.getParameter("parameterName")


回答4:

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.