|
六 03
|
Struts 中批量封装对象Struts 90Views 1 Comment »
|
首先创建代表产品的类cn.org.jock.Product,代码如下:
package cn.org.jock;
import java.sql.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;
}
}
本例子中的dateOfProduction属性使用了java.sql.Date,而不是java.util.Date。这是因为Struts 1.x不支持请求参数到java.util.Date的转换,归根到底是由于org.apache.commons.beanutils.ConvertUtilsBean.convert()不支持关于java.util.Date的转换。另外,值得注意的是common-beanutils是通过java.sql.Date.valueOf()方法工作的,所以在页面输入的字符串的格式必须为“yyyy-MM-dd”。
表单(Actoin Form)+ 列表(List)
首先,我们要创建类cn.org.jock.struts.util.AutoInitArrayList,代码如下:
/**
*
*/
package cn.org.jock.struts.util;
import java.util.ArrayList;
/**
* @author Jock
*
*/
public class AutoInitArrayList
private static final long serialVersionUID = 1L ;
private Class
public AutoInitArrayList(Class
this.t = t;
}
@Override
public E get( int index) {
try {
while (index >= size()) {
add(t.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
return super .get(index);
}
}
AutoInitArrayList继承ArrayList并重载get()方法,作用就是在Struts 1.x框架调用这个方法时,如果index超出列表大小,则会实例化新项放到列表中,避免出现(IndexOutOfBoundsException)异常。 阅读全文 »
近期评论