Struts2对象集合(批量输入)校验 详解

jopen 8年前

最后的目录结构:

Struts2对象集合(批量输入)校验 详解

addMaterial.jsp:

<%@ page language="java" pageEncoding="gb2312"%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  <!-- struts2标签库调用声明 -->  <%@taglib prefix="s" uri="/struts-tags"%>  <%@taglib prefix="sx" uri="/struts-dojo-tags"%>  <html>   <head>    <title>添加材料</title>    <s:head />    <sx:head />   </head>   <body>   <s:fielderror/>    <h3 align="left">      Struts2类型转换使用范例     </h3>    <!-- 材料输入表单 -->    <table>     <s:form id="materialForm" action="addMaterial" theme="simple">     <table>     <tr>     <td>材料名</td>     <td>价格</td>     <td>库存量</td>     <td>过期日期</td>     </tr>     <s:iterator value="new int[4]" status="m">     <tr>     <td><s:textfield name="%{'materialList['+#m.index+'].material'}" /></td>              <td><s:textfield name="%{'materialList['+#m.index+'].bid'}" /></td>              <td><s:textfield name="%{'materialList['+#m.index+'].mount'}" /></td>              <td><sx:datetimepicker name="%{'materialList['+#m.index+'].expireDate'}" /></td>     </tr>     </s:iterator>     <tr>     <td colspan="4"><s:submit value="提交"></s:submit>     </tr>     </table>      </s:form>    </table>    </body>  </html>

注释:关于此段代码详细解释见 Struts2 <s:lterator>标签的一个复杂应用 解析

最终提交的是一个名为materialList的集合实例,它有4个Material类型元素,每个元素都有material、bid、mount、expireDate这四个属性。

struts.xml:

<?xml version="1.0" encoding="gb2312"?>  <!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd">  <struts>   <!-- Action所在包定义 -->   <package name="Convert" extends="struts-default" >      <action name="addMaterial"     class="com.action.AddMaterialAction">     <result name="input">/jsp/addMaterial.jsp</result>     <result name="success">/jsp/showMaterial.jsp</result>    </action>    <action name="index">              <result>/jsp/addMaterial.jsp</result>          </action>   </package>  </struts>

注释:表单数据提交经struts.xml配置导航到action="addMaterial"的Action类AddMaterialAction去处理。

AddMaterialAction.java:

package com.action;  import java.util.List;  import com.model.Material;  import com.opensymphony.xwork2.ActionSupport;    public class AddMaterialAction extends ActionSupport {   private List<Material> materialList;      public String execute() throws Exception {    return SUCCESS;   }     public List<Material> getMaterialList() {    return materialList;   }     public void setMaterialList(List<Material> materialList) {    this.materialList = materialList;   }  }

注释:在JSP页面按"提交"按钮后,就相当于"setMaterialList()"方法,实例化一个materialList集合实例。

AddMaterialAction-conversion.properties:

Element_materialList=com.model.Material

注释:定义了List元素都是Material对象(由于定义时是用泛型List<Material>,此处是可缺省的)。

AddMaterialAction-validation:

<?xml version="1.0" encoding="gb2312"?>  <!DOCTYPE validators PUBLIC  "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  <validators>   <field name="materialList">    <field-validator type="visitor">     <param name="context">materialContext</param>     <param name="appendPrefix">true</param>     <message>添加材料输入校验:</message>    </field-validator>   </field>   </validators>

注释:这是Action类AddMaterialAction的校验配置文件,从命名就可看出来,它放在com.model目录下。其中的格式算是比较统一的,appendPrefix参数设置了true 即在出错信息前都加上message标签中的内容。context参数设置为"materialContext",Material类校验文件名“-”之间的名字要与之对应,本例为Material-materialContext-validation.xml。

Material.java:

package com.model;  import java.util.Date;    public class Material implements java.io.Serializable {   // Fields   private String material;   private double bid;   private int mount;   private Date expireDate;      public Material() {}     public double getBid() {    return bid;   }     public void setBid(double bid) {    this.bid = bid;   }     public int getMount() {    return mount;   }     public void setMount(int mount) {    this.mount = mount;   }     public String getMaterial() {    return this.material;   }     public void setMaterial(String material) {    this.material = material;   }     public Date getExpireDate() {    return expireDate;   }     public void setExpireDate(Date expireDate) {    this.expireDate = expireDate;   }   }

Material-materialContext-validation.xml:

<?xml version="1.0" encoding="gb2312"?>  <!DOCTYPE validators PUBLIC  "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  <validators>   <field name="material">    <field-validator type="requiredstring">     <message>请输入材料名</message>    </field-validator>   </field>   <field name="bid">    <field-validator type="double">     <param name="minExclusive">0.1</param>     <message>请输入价格</message>    </field-validator>   </field>   <field name="mount">    <field-validator type="int">     <param name="min">1</param>     <message>请输入库存量</message>    </field-validator>   </field>   <field name="expireDate">    <field-validator type="required">     <message>请输入过期日期</message>    </field-validator>    <field-validator type="date">     <param name="min">2009-01-01</param>     <param name="max">2019-01-01</param>     <message>输入过期日期无效</message>    </field-validator>   </field>  </validators>

注释:这是对于Material这个Java对象的输入校验配置文件,它放在com.model目录下。

struts.properties:

struts.i18n.encoding=gb2312

注释:使输入支持字符编码集GB2312。

showMaterial.jsp:

<%@ page language="java" pageEncoding="gb2312"%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  <!-- struts2标签库调用声明 -->  <%@taglib prefix="s" uri="/struts-tags"%>  <html>      <head>          <title>添加材料</title>          <s:head />      </head>      <body>          <!-- 材料数据显示 -->          <table>              <tr>              <td>材料名</td>              <td>价格</td>              <td>库存量</td>              <td>过期日期</td>              </tr>              <s:iterator value="materialList" status="m">              <tr>              <td><s:property value="material" ></s:property></td>              <td><s:property value="bid" ></s:property></td>              <td><s:property value="mount" ></s:property></td>              <td><s:property value="expireDate" ></s:property></td>              </tr>              </s:iterator>           </table>             </body>  </html>

注释:它完成提交的表单数据的显示。

地址栏运行 http://localhost:8080/Convert/jsp/addMaterial.jsp ,显示页面:

Struts2对象集合(批量输入)校验 详解

直接“提交”后,输出:

Struts2对象集合(批量输入)校验 详解

输入数据如下:

 Struts2对象集合(批量输入)校验 详解

结果:

Struts2对象集合(批量输入)校验 详解