当表单中存在数组时(假定依次输入了1,2,3):html
<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=3java
传统的Servlet只能接收数组中第一个参数。数组
publicclass ServletA extends HttpServlet { publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("param")); } }
Struts2则能接收多个参数并自动填充数组。this
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这样接收参数是没有问题的,可是最近发现这样子接收后台只能拿到数组的第一个参数,只能经过code
request.getParameterValues("xx");
来获取名字为xx的数组。能够经过修改xx变量的set方法来获取,即set方法改成如下内容便可。orm
this.xx=request.getParameterValues("xx");