当表单中存在数组时(假定依次输入了1,2,3):
<form>
<input type="text" name="param"/>
<input type="text" name="param"/>
<input type="text" name="param">
<input type="submit"/>
</form>
表单提交传递的字符串为:param=1¶m=2¶m=3
传统的Servlet只能接收数组中第一个参数。
publicclass ServletA extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("param"));
}
}
Struts2则能接收多个参数并自动填充数组。
publicclass A {
private String param[];
public String[] getParam() {
returnparam;
}
publicvoid setParam(String[] param) {
this.param = param;
}
publicvoid execute(){
for(int i=0;i<this.param.length;i++){
System.out.println(this.param[i]);
}
}
}
以上是网上大部分人的观点,之前struts2这样接收参数是没有问题的,但是最近发现这样子接收后台只能拿到数组的第一个参数,只能通过
request.getParameterValues("xx");
来获取名字为xx的数组。可以通过修改xx变量的set方法来获取,即set方法改为以下内容即可。
this.xx=request.getParameterValues("xx");
来源:oschina
链接:https://my.oschina.net/u/3696256/blog/1553443