[英]Spring MVC pass a list of values into controller from JSP page
If I have a HTML in a <form>
like this: 如果我在<form>
这样的HTML:
<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: 我知道在servlet中我可以使用以下方法获取值:
String[] values = request.getParameterValues("myValues");
How can I do something similar using Spring MVC? 如何使用Spring MVC做类似的事情?
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";
}
You already have HttpServletRequest request
in spring web MVC. 您已经在spring web MVC中拥有HttpServletRequest request
。 We can get it using the same ie using 我们可以使用相同的即使用它
String[] values = request.getParameterValues("myValues");
Also you can use ServletRequestUtils to read request parameters in Spring like : 您也可以使用ServletRequestUtils在Spring中读取请求参数,如:
String[] values = ServletRequestUtils.getRequiredStringParameters(request, "myValues");
See how to access Http request : Spring 3 MVC accessing HttpRequest from controller 了解如何访问Http请求: Spring 3 MVC从控制器访问HttpRequest
For annotation based approach: See Alex's answer. 对于基于注释的方法:请参阅Alex的答案。
In jsp i would like: 在jsp中我想:
<%@ 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. 参见13.9节中的参考文献。 Using Spring's form tag library tag explains, in link: http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html 使用Spring的表单标记库标记解释,链接: http : //static.springsource.org/spring/docs/2.0.x/reference/mvc.html
The controller is part of the same friend Alex answered. 控制器是Alex回答的同一个朋友的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.