最后的目录结构:
addMaterial.jsp:
01 |
<%@ page language="java" pageEncoding="gb2312"%> |
02 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
03 |
<!-- struts2标签库调用声明 --> |
04 |
<< a href = "mailto:%@taglib" >%@taglib</ a > prefix="s" uri="/struts-tags"%> |
05 |
<< a href = "mailto:%@taglib" >%@taglib</ a > prefix="sx" uri="/struts-dojo-tags"%> |
06 |
< html > |
07 |
< head > |
08 |
< title >添加材料</ title > |
09 |
< s:head /> |
10 |
< sx:head /> |
11 |
</ head > |
12 |
< body > |
13 |
< s:fielderror /> |
14 |
< h3 align = "left" > |
15 |
Struts2类型转换使用范例 |
16 |
</ h3 > |
17 |
<!-- 材料输入表单 --> |
18 |
< table > |
19 |
< s:form id = "materialForm" action = "addMaterial" theme = "simple" > |
20 |
< table > |
21 |
< tr > |
22 |
< td >材料名</ td > |
23 |
< td >价格</ td > |
24 |
< td >库存量</ td > |
25 |
< td >过期日期</ td > |
26 |
</ tr > |
27 |
< s:iterator value = "new int[4]" status = "m" > |
28 |
< tr > |
29 |
< td >< s:textfield name = "%{'materialList['+#m.index+'].material'}" /></ td > |
30 |
< td >< s:textfield name = "%{'materialList['+#m.index+'].bid'}" /></ td > |
31 |
< td >< s:textfield name = "%{'materialList['+#m.index+'].mount'}" /></ td > |
32 |
< td >< sx:datetimepicker name = "%{'materialList['+#m.index+'].expireDate'}" /></ td > |
33 |
</ tr > |
34 |
</ s:iterator > |
35 |
< tr > |
36 |
< td colspan = "4" >< s:submit value = "提交" ></ s:submit > |
37 |
</ tr > |
38 |
</ table > |
39 |
</ s:form > |
40 |
</ table > |
41 |
</ body > |
42 |
</ html > |
注释:关于此段代码详细解释见 Struts2 <s:lterator>标签的一个复杂应用 解析。
最终提交的是一个名为materialList的集合实例,它有4个Material类型元素,每个元素都有material、bid、mount、expireDate这四个属性。
struts.xml:
01 |
<? xml version = "1.0" encoding = "gb2312" ?> |
02 |
<!DOCTYPE struts PUBLIC |
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
04 |
"http://struts.apache.org/dtds/struts-2.0.dtd"> |
05 |
< struts > |
06 |
<!-- Action所在包定义 --> |
07 |
< package name = "Convert" extends = "struts-default" > |
08 |
< action name = "addMaterial" |
09 |
class = "com.action.AddMaterialAction" > |
10 |
< result name = "input" >/jsp/addMaterial.jsp</ result > |
11 |
< result name = "success" >/jsp/showMaterial.jsp</ result > |
12 |
</ action > |
13 |
< action name = "index" > |
14 |
< result >/jsp/addMaterial.jsp</ result > |
15 |
</ action > |
16 |
</ package > |
17 |
</ struts > |
注释:表单数据提交经struts.xml配置导航到action="addMaterial"的Action类AddMaterialAction去处理。
AddMaterialAction.java:
01 |
package com.action; |
02 |
import java.util.List; |
03 |
import com.model.Material; |
04 |
import com.opensymphony.xwork2.ActionSupport; |
05 |
06 |
public class AddMaterialAction extends ActionSupport { |
07 |
private List<Material> materialList; |
08 |
09 |
public String execute() throws Exception { |
10 |
return SUCCESS; |
11 |
} |
12 |
13 |
public List<Material> getMaterialList() { |
14 |
return materialList; |
15 |
} |
16 |
17 |
public void setMaterialList(List<Material> materialList) { |
18 |
this .materialList = materialList; |
19 |
} |
20 |
} |
注释:在JSP页面按"提交"按钮后,就相当于"setMaterialList()"方法,实例化一个materialList集合实例。
AddMaterialAction-conversion.properties:
Element_materialList=com.model.Material
注释:定义了List元素都是Material对象(由于定义时是用泛型List<Material>,此处是可缺省的)。
AddMaterialAction-validation:
01 |
<? xml version = "1.0" encoding = "gb2312" ?> |
02 |
<!DOCTYPE validators PUBLIC |
03 |
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" |
04 |
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> |
05 |
< validators > |
06 |
< field name = "materialList" > |
07 |
< field-validator type = "visitor" > |
08 |
< param name = "context" >materialContext</ param > |
09 |
< param name = "appendPrefix" >true</ param > |
10 |
< message >添加材料输入校验:</ message > |
11 |
</ field-validator > |
12 |
</ field > |
13 |
</ validators > |
注释:这是Action类AddMaterialAction的校验配置文件,从命名就可看出来,它放在com.model目录下。其中的格式算是比较统一的,appendPrefix参数设置了true 即在出错信息前都加上message标签中的内容。context参数设置为"materialContext",Material类校验文件名“-”之间的名字要与之对应,本例为Material-materialContext-validation.xml。
Material.java:
01 |
package com.model; |
02 |
import java.util.Date; |
03 |
04 |
public class Material implements java.io.Serializable { |
05 |
// Fields |
06 |
private String material; |
07 |
private double bid; |
08 |
private int mount; |
09 |
private Date expireDate; |
10 |
|
11 |
public Material() {} |
12 |
13 |
public double getBid() { |
14 |
return bid; |
15 |
} |
16 |
17 |
public void setBid( double bid) { |
18 |
this .bid = bid; |
19 |
} |
20 |
21 |
public int getMount() { |
22 |
return mount; |
23 |
} |
24 |
25 |
public void setMount( int mount) { |
26 |
this .mount = mount; |
27 |
} |
28 |
29 |
public String getMaterial() { |
30 |
return this .material; |
31 |
} |
32 |
33 |
public void setMaterial(String material) { |
34 |
this .material = material; |
35 |
} |
36 |
37 |
public Date getExpireDate() { |
38 |
return expireDate; |
39 |
} |
40 |
41 |
public void setExpireDate(Date expireDate) { |
42 |
this .expireDate = expireDate; |
43 |
} |
44 |
} |
Material-materialContext-validation.xml:
01 |
<? xml version = "1.0" encoding = "gb2312" ?> |
02 |
<!DOCTYPE validators PUBLIC |
03 |
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" |
04 |
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> |
05 |
< validators > |
06 |
< field name = "material" > |
07 |
< field-validator type = "requiredstring" > |
08 |
< message >请输入材料名</ message > |
09 |
</ field-validator > |
10 |
</ field > |
11 |
< field name = "bid" > |
12 |
< field-validator type = "double" > |
13 |
< param name = "minExclusive" >0.1</ param > |
14 |
< message >请输入价格</ message > |
15 |
</ field-validator > |
16 |
</ field > |
17 |
< field name = "mount" > |
18 |
< field-validator type = "int" > |
19 |
< param name = "min" >1</ param > |
20 |
< message >请输入库存量</ message > |
21 |
</ field-validator > |
22 |
</ field > |
23 |
< field name = "expireDate" > |
24 |
< field-validator type = "required" > |
25 |
< message >请输入过期日期</ message > |
26 |
</ field-validator > |
27 |
< field-validator type = "date" > |
28 |
< param name = "min" >2009-01-01</ param > |
29 |
< param name = "max" >2019-01-01</ param > |
30 |
< message >输入过期日期无效</ message > |
31 |
</ field-validator > |
32 |
</ field > |
33 |
</ validators > |
注释:这是对于Material这个Java对象的输入校验配置文件,它放在com.model目录下。
struts.properties:
struts.i18n.encoding=gb2312
注释:使输入支持字符编码集GB2312。
showMaterial.jsp:
01 |
<%@ page language="java" pageEncoding="gb2312"%> |
02 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
03 |
<!-- struts2标签库调用声明 --> |
04 |
<%@taglib prefix="s" uri="/struts-tags"%> |
05 |
< html > |
06 |
< head > |
07 |
< title >添加材料</ title > |
08 |
< s:head /> |
09 |
</ head > |
10 |
< body > |
11 |
<!-- 材料数据显示 --> |
12 |
< table > |
13 |
< tr > |
14 |
< td >材料名</ td > |
15 |
< td >价格</ td > |
16 |
< td >库存量</ td > |
17 |
< td >过期日期</ td > |
18 |
</ tr > |
19 |
< s:iterator value = "materialList" status = "m" > |
20 |
< tr > |
21 |
< td >< s:property value = "material" ></ s:property ></ td > |
22 |
< td >< s:property value = "bid" ></ s:property ></ td > |
23 |
< td >< s:property value = "mount" ></ s:property ></ td > |
24 |
< td >< s:property value = "expireDate" ></ s:property ></ td > |
25 |
</ tr > |
26 |
</ s:iterator > |
27 |
</ table > |
28 |
</ body > |
29 |
</ html > |
注释:它完成提交的表单数据的显示。
地址栏运行 http://localhost:8080/Convert/jsp/addMaterial.jsp ,显示页面:
直接“提交”后,输出:
输入数据如下:
结果: