6

If I have a HTML in a <form> like this:

    <input type="text" value="someValue1" name="myValues"/>
    <input type="text" value="someValue2" name="myValues"/>
    <input type="text" value="someValue3" name="myValues"/>
    <input type="text" value="someValue4" name="myValues"/>

I know that in servlets I am able to get values using:

String[] values = request.getParameterValues("myValues");

How can I do something similar using Spring MVC?

3 Answers3

7

The parameters are passed as arguments to the method bound to your controller

@RequestMapping(value = "/foo", method = RequestMethod.POST) // or GET
public String foo(@RequestParam("myValues") String[] myValues) {
    // Processing
    return "view";
}
Alex
  • 25,147
  • 6
  • 59
  • 55
2

You already have HttpServletRequest request in spring web MVC. We can get it using the same i.e. using

 String[] values = request.getParameterValues("myValues");

Also you can use ServletRequestUtils to read request parameters in Spring like :

String[] values = ServletRequestUtils.getRequiredStringParameters(request, "myValues");

See how to access Http request : Spring 3 MVC accessing HttpRequest from controller

For annotation based approach: See Alex's answer.

Community
  • 1
  • 1
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

In jsp i would like:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!-- Add taglib in beginning of the page.-->

<!-- In body the html -->
<form:form  action="/foo" method="post" commandName="MyValues">
  <input type="text" value="someValue1" name="myValues1"/>
  <input type="text" value="someValue2" name="myValues2"/>
  <input type="text" value="someValue3" name="myValues3"/>
  <input type="text" value="someValue4" name="myValues4"/>
</form:form>

See references in section 13.9. Using Spring's form tag library tag explains, in link: http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

The controller is part of the same friend Alex answered.

Dalton Dias
  • 76
  • 1
  • 4
  • 9