这个就是Struts的数据验证问题(当然,有些也可以通过赋初始值办法解决),数据验证有两种方法,
在struts中,ActionForm验证方法采用如下格式:
public ActionErrors validate(ActionMapping mapping,
                             HttpServletRequest request) {
      ActionErrors errors = new ActionErrors();
      if((userName == null) || (userName.length() < 1)) {
         errors.add("userName",
             new ActionError("error.userName.required"));
      }
      if((password == null) || (password.length() < 1)) {
         errors.add("password",
             new ActionError("error.password.required"));
      }      return errors;
   }---------------------------------------------------------------------------
在struts中,js验证采用如下格式:
<html:form  action="/UserLoginAction" focus="username" onsubmit="return validateLogonForm(this);">
...................
<html:javascript formName="logonForm" dynamicJavascript="true" staticJavascript="false"  />
<script language=Javascript1.1 src="staticJavascript.jsp"></script>
.............
其中logonForm在validation.xml中定义:
 <form name="logonForm">            <field property="username"
                    depends="required, minlength,maxlength">
                <arg0   key="prompt.username"/>
                <arg1   key="${var:minlength}" name="minlength"
                   resource="false"/>
                <arg2   key="${var:maxlength}" name="maxlength"
                   resource="false"/>
                <var>
                    <var-name>maxlength</var-name>
                    <var-value>16</var-value>
                </var>
                <var>
                    <var-name>minlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>            <field property="password"
                    depends="required, minlength,maxlength"
                    bundle="alternate">
                <arg0   key="prompt.password"/>
                <arg1   key="${var:minlength}" name="minlength"
                   resource="false"/>
                <arg2   key="${var:maxlength}" name="maxlength"
                   resource="false"/>
                <var>
                    <var-name>maxlength</var-name>
                    <var-value>16</var-value>
                </var>
                <var>
                    <var-name>minlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>        </form>
对应的js代码在validator-rules.xml中定义:
<validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionErrors,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required">         <javascript>&lt;![CDATA[
            function validateRequired(form) {
                var isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oRequired = new required();
                for (x in oRequired) {
                 var field = form[oRequired[x][0]];
                
                    if (field.type == 'text' ||
                        field.type == 'textarea' ||
                        field.type == 'file' ||
                        field.type == 'select-one' ||
                        field.type == 'radio' ||
                        field.type == 'password') {
                        
                        var value = '';
// get field's value
if (field.type == "select-one") {
var si = field.selectedIndex;
if (si >= 0) {
value = field.options[si].value;
}
} else {
value = field.value;
}
                        
                        if (value == '') {
                        
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oRequired[x][1];
                        isValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return isValid;
            }]]&gt;
         </javascript>      </validator>
---------------------------------------
 mickey_uuu():再补充点:
  1自定义的ActionForm不在继承ActionForm 改为ValidatorForm;  2修改struts_config.xml:添加资源文件 plugin
    在<struts-config>和</struts-config>之间添加
      <message-resources parameter="resources.application" />
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" property="pathnames" />
  </plug-in>
   3 将validator-rules.xml,validation.xml拷贝到web-inf下
   4 在web-inf/classed/resources/下建立 application.properties文件;

解决方案 »

  1.   

    在action的相应位子写个if语句例如:if(参数 !=null){......}
      

  2.   

    我刚刚学到一个不知是否number的能否用
    String a=(a!=null?a:"");
      

  3.   

    感谢各位,特别是kui(kui),使我又懂得了使用validator,不过似乎只有这句说到了点子上:“如果没别的办法就用字符串类型,在程序中再进行类型转换”ActionForm有个属性sign是Integer型,
    首先在ActionForm里我是这样定义的:
          private Integer sign = null;
          public Integer getSign()
          {
                return sign;
          }
          public void setSign(Integer aSign)
          {
                sign = aSign;
          }在JSP页面上是这样的:
    <html:text property="sign"/>这样页面第一页刷新时我在Action中输出:
    System.out.println("form.getSign():" + form.getSign());
    值为null;但我提交数据后,
    System.out.println("form.getSign():" + form.getSign());
    值成了0,实际上我在页面上什么也没有输,struts自动转成了0
    为什么不是null?不解啊,我又没输0,非要使用String型吗?String型也是一个""空串,而不是null
      

  4.   

    提交之后Form的sign值成了一个不为null的Integer,它的值是0,为什么会这样?可不可以设置默认为null?
      

  5.   

    在web.xml中给ActionServlet添加属性convertNull.
    具体参考文档