0

Generally we can get in the controller the posted values of the fields from JSP like this :

@Controller
public class someClass {

    @RequestMapping(value = "/someUrl", method = RequestMethod.POST)
    public ModelAndView someMethodName(@RequestParam Map<String, String> params) {

        for (Map.Entry<String, String> param : params.entrySet()) {

            // field name is got from param.getKey() , field value is got from param.getValue()

        }

        return new ModelAndView("redirect:/someOtherUrl");

    }

}

The problem occurs if a field is a select element which is multiple. So how to get the values selected from it ?

pheromix
  • 18,213
  • 29
  • 88
  • 158

1 Answers1

1

Edit: How to get multiple selected values from select box in JSP? has the right answer. Use a mapping of List<String> to obtain your results.

I'll leave this here, because this is right as well ;-) Don't iterate over entries, but use the keySet. Those won't be duplicate by design.

i.e.:

for (String key: params.keySet()) {
    // field name key, field value is params.get(key)
}
Community
  • 1
  • 1
rob
  • 212
  • 3
  • 8
  • I always got just one value even if I select more than one options ! NB : I tested with the `name` attribute of the select element with and without the `[ ]` – pheromix Mar 04 '17 at 12:49
  • Oh, I misunderstood. According to http://stackoverflow.com/questions/2407945/how-to-get-multiple-selected-values-from-select-box-in-jsp I would try to use a Map or a List instead of Map – rob Mar 04 '17 at 13:32
  • you should change your answer because your last comment mentionning the link is the right answer :) – pheromix Mar 04 '17 at 13:45
  • 1
    Cool, glad I could help :-) – rob Mar 04 '17 at 13:51