DemoAction.java:
package cn.xxx.c_param;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
//struts2 封装集合类型参数
public class DemoAction extends ActionSupport {
//list 接收到的参数封装到List集合中
private List<String> list; // 接收参数的方式:属性驱动。 属性要有对应的getter和setter方法。
//Map 接收到的参数封装到Map集合中
private Map<String,String> map;
public String execute() throws Exception {
System.out.println("list:"+list);
System.out.println("map:"+map);
return SUCCESS;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
form1.jsp(对应的form表单):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/DemoAction" method="post" >
list:<input type="text" name="list" /><br> <!-- 会封装到List集合中,顺序一个一个挨着排 -->
list:<input type="text" name="list[3]" /><br> <!-- 会封装到List集合中的第四个元素的位置 -->
map:<input type="text" name="map['键']" /><br> <!-- 会封装到Map集合对应的键上 -->
<input type="submit" value="提交" />
</form>
</body>
</html>