struts2[2.3]参数获得方式-(4)集合类型参数封装(list和map)
1.学习路线
今天咱们来学struts2参数获得方式,let`go!
图1.学习路线
图2.类和配置文件
form4.jsp,写一个表单,用于提交:
<%@ 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}/Demo11Action" method="post">
list:<input type="text" name="list"><br>
<!-- 可提交到集合的指定索引上 -->
list:<input type="text" name="list[1]"><br>
<!-- 把表单上的值提交到键为:haha的map上 -->
map:<input type="text" name="map['haha']"><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
2.获得参数
新建一个Demo11Action类,继承ActionSupport,再创建一个execute()方法,return SUCCESS(创建集合类型属性的Getters()和Setters()方法)。
package cn.aisino.c_param;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.aisino.domain.User;
//struts2封装集合类型参数
public class Demo11Action extends ActionSupport{
private List<String> list;
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;
}
}
在包下的配置文件struts.xml中配置Action,
在主配置文件struts.xml中配置,
启动服务器,在地址栏中访问form4.jsp,输入数据:
提交,控制台可查看数据成功提交: