本文是针对Struts2.0中的使用List进行批量封装而写的,用代码来诠释某些东东。因为刚开始学习struts2.0,以后会进行补充这方面只是的。 需求是:我将产品信息批量增加,并显示结果:
1。建立实体bean :Product
package demo;
import java.util.Date;
public class Product {
private String name;
private double price;
private Date dateOfProduction ;
public Date getDateOfProduction(){
return dateOfProduction;
}
public void setDateOfProduction(Date dateOfProduction) {
this.dateOfProduction = dateOfProduction;
}
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;
}
}
2.控制业务逻辑的Action类: ProductConfirm
package demo;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class ProductConfirm extends ActionSupport {
public List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public String execute(){
if(products!=null){
for(Product p : products){
con.Convert(p.getName());
System.out.println("p.name=="+p.getName()+" | "+ p.getPrice()+" | "+p.getDateOfProduction());
}
}else{
System.out.println("product is null");
}
System.out.println();
System.out.println();
System.out.println();
return SUCCESS;
}
}
3.AddProducts.jsp 添加产品的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> AddProducts.jsp </title>
</head>
<body>
<s:form action="ProductConfirm" theme="simple" >
<table>
<tr style="background-color:powderblue ;font-weight:bold;">
<td>Product_Name</td>
<td>Price</td>
<td>Date</td>
</tr>
<%--
<tr>
<td>
<input type="text" name="products[0].name" />
</td>
<td>
<input type="text" name="products[0].price">
</td>
<td>
<input name="products[0].dateOfProduction" type="text"/>
</td>
</tr>
--%>
<s:iterator value="new int[3]" status="stat">
<tr>
<td>
<s:textfield name="%{'products['+#stat.index+'].name'}"/>
</td>
<td> <s:textfield name="%{'products['+#stat.index+'].price'}"/>
</td>
<TD><s:textfield name="%{'products['+#stat.index+'].dateOfProduction'}"/>
</TD>
</tr>
</s:iterator>
<tr>
<td colspan="3"> <s:submit/> </td>
</tr>
</table>
</s:form>
<br>
</body>
</html>
4. 展示添加产品的页面:ShowProducts.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> ShowProducts.jsp </title>
</head>
<body>
<table>
<tr style="background-color:powderblue;font-weight:bold;">
<td>Product_Name</td>
<td>Price</td>
<td>Date</td>
</tr>
<s:iterator value="products" >
<tr>
<td><s:property value="name"/> </td>
<td><s:property value="price"/> </td>
<td><s:property value="dateOfProduction" /> </td>
</tr>
</s:iterator>
</table> <br>
</body>
</html>
5. struts.xml 的配置
<action name="ProductConfirm" class="demo.ProductConfirm">
<result>/ShowProducts.jsp</result>
</action>
展示结果页面:参考上传的图片
说明:在AddProducts.jsp的<s:textfield>的name为“%{'products['+#stat.index+'].name'}”,%{exp}格式表示使用OGNL表达式,上述表达式的相当于<%= "products[" + stat.index + "].name" %>