struts2 获取Action中的数据
package cn.itcast;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import domain.Product;
public class ProductAction {
//第二种方法,属性驱动,创建私有化属性.因有getList方法.
private List<Product> list;
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
public String showProduct(){
list = new ArrayList<>();
Product p1 = new Product();
p1.setName("电冰箱");
p1.setCount(100);
p1.setPrice(1000);
Product p2 = new Product();
p2.setName("电视机");
p2.setCount(50);
p2.setPrice(500);
list.add(p1);
list.add(p2);
//第一种方法.将数据存到ValueStack中
ActionContext.getContext().getValueStack().set("list", list);
return "success";
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<body>
<table border="1">
<tr>
<td>商品名称</td>
<td>商品数量</td>
<td>商品价格</td>
</tr>
//遍历集合
<s:iterator value="list">
<tr>
<td><s:property value="name"/></td>
<td><s:property value="count"/></td>
<td><s:property value="price"/></td>
</tr>
</s:iterator>
</table>
</body>

浙公网安备 33010602011771号