目录
一 、复杂数据类型的封装:封装数据到List集合中
二 、复杂数据类型的封装:封装数据到Map集合
编写实体类 Product
package czm.struts2.domain;
public class Product {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + "]";
}
}
一 、复杂数据类型的封装:封装数据到List集合中
1 、编写 JSP 页面
<h1>Struts2的复杂类型的数据封装</h1>
<h3>封装到List集合中:批量插入商品</h3>
<form action="${ pageContext.request.contextPath }/productAction1.action" method="post">
商品名称:<input type="text" name="products[0].name"><br/>
商品价格:<input type="text" name="products[0].price"><br/>
商品名称:<input type="text" name="products[1].name"><br/>
商品价格:<input type="text" name="products[1].price"><br/>
商品名称:<input type="text" name="products[2].name"><br/>
商品价格:<input type="text" name="products[2].price"><br/>
<input type="submit" value="提交">
</form>
2 、编写 Action 类(提供集合的get、set方法)
package czm.struts2.demo3;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import czm.struts2.domain.Product;
/**
* 复杂类型的数据封装:封装到List集合
* @author ASUS
*
*/
public class ProductAction1 extends ActionSupport {
private List<Product> products;
// 提供集合的get、set方法:
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
@Override
public String execute() throws Exception {
for (Product product : products) {
System.out.println(product);
}
return NONE;
}
}
3 、编写 xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo3" extends="struts-default" namespace="/">
<!-- 封装数据到List集合中的配置 -->
<action name="productAction1" class="czm.struts2.demo3.ProductAction1"></action>
</package>
</struts>
4 、访问 JSP 页面
5 、输入信息,点击购买
二 、复杂数据类型的封装:封装数据到Map集合
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>
<h1>Struts2的复杂类型的数据封装</h1>
<h3>封装到List集合中:批量插入商品</h3>
<form action="${ pageContext.request.contextPath }/productAction1.action" method="post">
商品名称:<input type="text" name="products[0].name"><br/>
商品价格:<input type="text" name="products[0].price"><br/>
商品名称:<input type="text" name="products[1].name"><br/>
商品价格:<input type="text" name="products[1].price"><br/>
商品名称:<input type="text" name="products[2].name"><br/>
商品价格:<input type="text" name="products[2].price"><br/>
<input type="submit" value="购买">
</form>
<h3>封装到Map集合中:批量插入商品</h3>
<form action="${ pageContext.request.contextPath }/productAction2.action" method="post">
商品名称:<input type="text" name="map['one'].name"><br/>
商品价格:<input type="text" name="map['one'].price"><br/>
商品名称:<input type="text" name="map['two'].name"><br/>
商品价格:<input type="text" name="map['two'].price"><br/>
商品名称:<input type="text" name="map['three'].name"><br/>
商品价格:<input type="text" name="map['three'].price"><br/>
<input type="submit" value="购买">
</form>
</body>
</html>
2 、编写 Action 类
package czm.struts2.demo3;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import czm.struts2.domain.Product;
/**
* 复杂数据类型的封装:封装到Map集合
* @author ASUS
*
*/
public class ProductAction2 extends ActionSupport {
private Map<String, Product> map;
public Map<String, Product> getMap() {
return map;
}
public void setMap(Map<String, Product> map) {
this.map = map;
}
@Override
public String execute() throws Exception {
for (String key : map.keySet()) {
Product product = map.get(key);
System.out.println(key+" "+product);
}
return NONE;
}
}
3 、编写 xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo3" extends="struts-default" namespace="/">
<!-- 复杂数据类型的封装:封装数据到List集合中的配置 -->
<action name="productAction1" class="czm.struts2.demo3.ProductAction1"></action>
<!-- 复杂数据类型的封装:封装数据到Map集合的配置 -->
<action name="productAction2" class="czm.struts2.demo3.ProductAction2"></action>
</package>
</struts>
4 、访问 JSP 页面
5 、输入信息,点击购买