批量發布產品信息
圖
圖
具體實現
首先創建代表產品的類 Product
package tipsAndTricks;
import java
public class Product {
private String name;
private double price;
private Date dateOfProduction;
public Date getDateOfProduction() {
return dateOfProduction;
}
public void setDateOfProduction(Date dateOfProduction) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public double getPrice() {
return price;
}
public void setPrice( double price) {
this
}
}
package tipsAndTricks;
import java
public class Product {
private String name;
private double price;
private Date dateOfProduction;
public Date getDateOfProduction() {
return dateOfProduction;
}
public void setDateOfProduction(Date dateOfProduction) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public double getPrice() {
return price;
}
public void setPrice( double price) {
this
}
}
本例子中的dateOfProduction屬性使用了java
實現上述功能大概有三種方法
方法一
首先
view plaincopy to clipboardprint?
<? xml version=
<! DOCTYPE struts
< struts
< data
< form
< form
type =
< form
type =
</ form
</ form
< global
< global
< action
< action attribute =
name =
scope =
validate =
< forward name =
</ action >
</ action
< message
</ struts
<? xml version=
<! DOCTYPE struts
< struts
< data
< form
< form
type =
< form
type =
</ form
</ form
< global
< global
< action
< action attribute =
name =
scope =
validate =
< forward name =
</ action >
</ action
< message
</ struts
view plaincopy to clipboardprint?
<% @ page language =
<% @ taglib uri =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > Add Products </ title >
</ head >
< body >
< html:form action =
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach var =
< tr >
< td >< html:text indexed =
< td >< html:text indexed =
< td >< html:text indexed =
</ tr >
</ c:forEach >
< tr >
< td colspan =
</ tr >
</ table >
</ html:form >
</ body >
</ html >
<% @ page language =
<% @ taglib uri =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > Add Products </ title >
</ head >
< body >
< html:form action =
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach var =
< tr >
< td >< html:text indexed =
< td >< html:text indexed =
< td >< html:text indexed =
</ tr >
</ c:forEach >
< tr >
< td colspan =
</ tr >
</ table >
</ html:form >
</ body >
</ html > 例中
然後
view plaincopy to clipboardprint?
<% @ page language =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > View Products </ title >
</ head >
< body >
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach var =
< tr >
< td > ${product
< td > ${product
< td > ${product
</ tr >
</ c:forEach >
</ table >
</ body >
</ html >
<% @ page language =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > View Products </ title >
</ head >
< body >
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach var =
< tr >
< td > ${product
< td > ${product
< td > ${product
</ tr >
</ c:forEach >
</ table >
</ body >
</ html > 我想這份也不多作說明
最後是建立Action文件 BatchWrappingWithArrayAction
view plaincopy to clipboardprint?
package tipsAndTricks;
import javax
import javax
import org
import org
import org
import org
import org
public class BatchWrappingWithArrayAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
DynaActionForm dynaProductsForm = (DynaActionForm) form;
request
return mapping
}
}
package tipsAndTricks;
import javax
import javax
import org
import org
import org
import org
import org
public class BatchWrappingWithArrayAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
DynaActionForm dynaProductsForm = (DynaActionForm) form;
request
return mapping
}
} 此Action將動態表單傳過來的products數組放到request中
在/addProducts
方法二
方法一雖然簡單
首先
view plaincopy to clipboardprint?
package tipsAndTricks;
import java
public class AutoInitArrayList < T > extends ArrayList < T > {
private static final long serialVersionUID =
private Class < T > t = null ;
public AutoInitArrayList(Class < T > t) {
this
}
@Override
public T get( int index) {
try {
while (index >= size()) {
add(t
}
} catch (Exception e) {
e
}
return super
}
}
package tipsAndTricks;
import java
public class AutoInitArrayList < T > extends ArrayList < T > {
private static final long serialVersionUID =
private Class < T > t = null ;
public AutoInitArrayList(Class < T > t) {
this
}
@Override
public T get( int index) {
try {
while (index >= size()) {
add(t
}
} catch (Exception e) {
e
}
return super
}
} AutoInitArrayList繼承ArrayList並重載get()方法
接著
view plaincopy to clipboardprint?
<? xml version=
<! DOCTYPE struts
< struts
< data
< form
< form
type =
< form
type =
</ form
< form
type =
</ form
< global
< global
< action
< action attribute =
name =
scope =
validate =
< forward name =
</ action >
< action attribute =
name =
type =
< forward name =
</ action >
</ action
< message
</ struts
<? xml version=
<! DOCTYPE struts
< struts
< data
< form
< form
type =
< form
type =
</ form
< form
type =
</ form
< global
< global
< action
< action attribute =
name =
scope =
validate =
< forward name =
</ action >
< action attribute =
name =
type =
< forward name =
</ action >
</ action
< message
</ struts
view plaincopy to clipboardprint?
package tipsAndTricks;
import java
import org
public class NormalProductsForm extends ActionForm {
private List products = new AutoInitArrayList < Product > (Product
public List getProducts() {
return products;
}
public void setProducts(List products) {
this
}
package tipsAndTricks;
import java
import org
public class NormalProductsForm extends ActionForm {
private List products = new AutoInitArrayList < Product > (Product
public List getProducts() {
return products;
}
public void setProducts(List products) {
this
}
} 接下來是Action類 BatchWrappingNormalAction
view plaincopy to clipboardprint?
/**/ /*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package tipsAndTricks;
import javax
import javax
import org
import org
import org
import org
public class BatchWrappingNormalAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
NormalProductsForm normalProductsForm = (NormalProductsForm) form;
request
return mapping
}
}
/**/ /*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package tipsAndTricks;
import javax
import javax
import org
import org
import org
import org
public class BatchWrappingNormalAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
NormalProductsForm normalProductsForm = (NormalProductsForm) form;
request
return mapping
}
} 基本上與方法一的Action一樣
view plaincopy to clipboardprint?
<% @ page language =
<% @ taglib uri =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > Add Products </ title >
</ head >
< body >
< html:form action =
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach begin =
< tr >
< td >< input name =
< td >< input name =
< td >< input name =
</ tr >
</ c:forEach >
< tr >
< td colspan =
</ tr >
</ table >
</ html:form >
</ body >
</ html >
<% @ page language =
<% @ taglib uri =
<% @ taglib uri =
<! DOCTYPE HTML PUBLIC
< html >
< head >
< title > Add Products </ title >
</ head >
< body >
< html:form action =
< table border =
< tr style =
< td > Product Name </ td >
< td > Price </ td >
< td > Date of production </ td >
</ tr >
< c:forEach begin =
< tr >
< td >< input name =
< td >< input name =
< td >< input name =
</ tr >
</ c:forEach >
< tr >
< td colspan =
</ tr >
</ table >
</ html:form >
</ body >
</ html > /addProducts
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28917.html