java Spring MVC 将值列表从 JSP 页面传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13602061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC pass a list of values into controller from JSP page
提问by Alex
If I have a HTML in a <form>
like this:
如果我有这样的 HTML <form>
:
<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 做类似的事情?
回答by Alex
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";
}
回答by Nandkumar Tekale
You already have HttpServletRequest request
in spring web MVC. We can get it using the same i.e. using
你已经HttpServletRequest request
在 spring web MVC 中了。我们可以使用相同的即使用
String[] values = request.getParameterValues("myValues");
Also you can use ServletRequestUtilsto 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 的回答。
回答by Dalton Dias
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. Using Spring's form tag library tag explains, in link: http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
请参阅第 13.9 节中的参考资料。使用 Spring 的表单标签库标签说明,在链接中:http: //static.springsource.org/spring/docs/2.0.x/reference/mvc.html
The controller is part of the same friend Alex answered.
控制器是亚历克斯回答的同一个朋友的一部分。