最后的目录结构:
![Struts2对象集合(批量输入)校验 详解](https://i-blog.csdnimg.cn/blog_migrate/481f32e737c30f448465b9fbd0eb5a8d.png)
addMaterial.jsp:
01 | <%@ page language="java" pageEncoding="gb2312"%> |
02 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
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"%> |
19 | < s:form id = "materialForm" action = "addMaterial" theme = "simple" > |
27 | < s:iterator value = "new int[4]" status = "m" > |
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 > |
36 | < td colspan = "4" >< s:submit value = "提交" ></ s:submit > |
注释:关于此段代码详细解释见 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"> |
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 > |
14 | < result >/jsp/addMaterial.jsp</ result > |
注释:表单数据提交经struts.xml配置导航到action="addMaterial"的Action类AddMaterialAction去处理。
AddMaterialAction.java:
02 | import java.util.List; |
03 | import com.model.Material; |
04 | import com.opensymphony.xwork2.ActionSupport; |
06 | public class AddMaterialAction extends ActionSupport { |
07 | private List<Material> materialList; |
09 | public String execute() throws Exception { |
13 | public List<Material> getMaterialList() { |
17 | public void setMaterialList(List<Material> materialList) { |
18 | this .materialList = materialList; |
注释:在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"> |
06 | < field name = "materialList" > |
07 | < field-validator type = "visitor" > |
08 | < param name = "context" >materialContext</ param > |
09 | < param name = "appendPrefix" >true</ param > |
10 | < message >添加材料输入校验:</ message > |
注释:这是Action类AddMaterialAction的校验配置文件,从命名就可看出来,它放在com.model目录下。其中的格式算是比较统一的,appendPrefix参数设置了true 即在出错信息前都加上message标签中的内容。context参数设置为"materialContext",Material类校验文件名“-”之间的名字要与之对应,本例为Material-materialContext-validation.xml。
Material.java:
02 | import java.util.Date; |
04 | public class Material implements java.io.Serializable { |
06 | private String material; |
09 | private Date expireDate; |
13 | public double getBid() { |
17 | public void setBid( double bid) { |
21 | public int getMount() { |
25 | public void setMount( int mount) { |
29 | public String getMaterial() { |
33 | public void setMaterial(String material) { |
34 | this .material = material; |
37 | public Date getExpireDate() { |
41 | public void setExpireDate(Date expireDate) { |
42 | this .expireDate = expireDate; |
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"> |
06 | < field name = "material" > |
07 | < field-validator type = "requiredstring" > |
08 | < message >请输入材料名</ message > |
12 | < field-validator type = "double" > |
13 | < param name = "minExclusive" >0.1</ param > |
14 | < message >请输入价格</ message > |
18 | < field-validator type = "int" > |
19 | < param name = "min" >1</ param > |
20 | < message >请输入库存量</ message > |
23 | < field name = "expireDate" > |
24 | < field-validator type = "required" > |
25 | < message >请输入过期日期</ message > |
27 | < field-validator type = "date" > |
28 | < param name = "min" >2009-01-01</ param > |
29 | < param name = "max" >2019-01-01</ param > |
30 | < message >输入过期日期无效</ message > |
注释:这是对于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"> |
04 | <%@taglib prefix="s" uri="/struts-tags"%> |
19 | < s:iterator value = "materialList" status = "m" > |
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 > |
注释:它完成提交的表单数据的显示。
地址栏运行 http://localhost:8080/Convert/jsp/addMaterial.jsp ,显示页面:
![Struts2对象集合(批量输入)校验 详解](https://i-blog.csdnimg.cn/blog_migrate/6ebef12afe2f687ecf6a622a08182b71.png)
直接“提交”后,输出:
![Struts2对象集合(批量输入)校验 详解](https://i-blog.csdnimg.cn/blog_migrate/dd9303f88467f81d2a423c88c5887698.png)
输入数据如下:
![Struts2对象集合(批量输入)校验 详解](https://i-blog.csdnimg.cn/blog_migrate/a122acce083f80cf26129bb113800e7c.png)
结果:
![Struts2对象集合(批量输入)校验 详解](https://i-blog.csdnimg.cn/blog_migrate/f006c5af77eb36989eb9e40c10798776.png)