7.struts2数据封装到集合中(list map)
**Struts2把数据封装到集合中(记住页面的写法)**
1.封装复杂类型的参数(集合类型 Collection 、Map接口等)
2.需求:页面中有可能想批量添加一些数据,那么现在就可以使用上述的技术了。把数据封装到集合中。
3.把数据封装到Collection中
* 因为Collection接口都会有下标值,所有页面的写法会有一些区别,注意:
> <input type="text" name="products[0].name" />
* 在Action中的写法,需要提供products的集合,并且提供get和set方法。
4.把数据封装到Map中
* Map集合是键值对的形式,页面的写法
> <input type="text" name="map['one'].name" />
* Action中提供map集合,并且提供get和set方法。
5.在Action中不推荐使用模型驱动的方式来封装数据到集合中,太麻烦,基本都选择第二种方式来封装。
6.提供的JSP页面
<form action="${ pageContext.request.contextPath }/xxx.action" method="post">
商品名称:<input type="text" name="" /><br/>
商品价格:<input type="text" name="" /><br/>
商品名称:<input type="text" name="" /><br/>
商品价格:<input type="text" name="" /><br/>
<input type="submit" value="添加" />
</form>
7.总结
* 注意:页面的写法
* 如果封装的List集合,采用下标的写法
* 如果封装的是map集合,自己知道key值
demo1_1.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>
<h3>数据封装到集合中(List集合)</h3>
<form action="${ pageContext.request.contextPath }/product1.action" method="post">
<!-- 封装到一个商品的对象中 -->
商品名称:<input type="text" name="products[0].pname" /><br/>
商品价格:<input type="text" name="products[0].money" /><br/>
<!-- 封装到一个商品的对象中 -->
商品名称:<input type="text" name="products[1].pname" /><br/>
商品价格:<input type="text" name="products[1].money" /><br/>
<input type="submit" value="添加" />
</form>
<h3>数据封装到集合中(map集合)</h3>
<form action="${ pageContext.request.contextPath }/product2.action" method="post">
<!-- 封装到一个商品的对象中 -->
商品名称:<input type="text" name="map['one'].pname" /><br/>
商品价格:<input type="text" name="map['one'].money" /><br/>
<!-- 封装到一个商品的对象中 -->
商品名称:<input type="text" name="map['two'].pname" /><br/>
商品价格:<input type="text" name="map['two'].money" /><br/>
<input type="submit" value="添加" />
</form>
</body>
</html>
Product:
package demo1;
public class Product {
private String pname;
private double money;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String toString() {
return "Product [pname=" + pname + ", money=" + money + "]";
}
}
数据封装到List:
package demo1;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
* 封装到List集合中
* @author mjl
*
*/
public class ProductAction extends ActionSupport{
//提供集合
private List<Product> products=new ArrayList<>();
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public String add(){
for(Product p :products){
System.out.println(p);
}
return NONE;
}
}
数据封装到Map:
package demo1;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
/**
* 数据封装到map集合中
* @author mjl
*
*/
public class ProductAction2 extends ActionSupport{
//定义Map集合,初始化
private Map<String,Product> map=new HashMap<>();
public Map<String, Product> getMap() {
return map;
}
public void setMap(Map<String, Product> map) {
this.map = map;
}
public String add(){
System.out.println(map);
return NONE;
}
}

浙公网安备 33010602011771号