回 帖 发 新 帖 刷新版面

主题:[讨论]myeclipse6.0+struts1.2中的validator用法????

这是validator-rules.xml文件,在WEB-INF目录下




<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>

   <global>

      <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.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>

      ……


     <!--
       This simply allows struts to include the validateUtilities into a page, it should
       not be used as a validation rule.
     -->
     <validator name="includeJavaScriptUtilities"
            classname=""
               method=""
         methodParams=""
              depends=""
                  msg=""
           jsFunction="org.apache.commons.validator.javascript.validateUtilities"/>

   </global>

</form-validation>

回复列表 (共3个回复)

沙发

这个是validation.xml,自己写的,也在WEB-INF目录下




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >

<form-validation>
   <formset>
     <form name="setVariablessForm">
      <field property="cash" depends="integer">
       <arg0 key="cash"/>
       <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
       <var>
         <var-name>maxlength</var-name>
         <var-value>4</var-value>
       </var>
     </field>
             
     </form>
   </formset>
</form-validation>






板凳

这个是jsp文件:


<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html> 
    <head>
        <title>JSP for SetVariablessForm form</title>
    </head>
    <body>
        <html:form action="/setVariabless"onsubmit="return SetVariablessForm

(this);"> 
现金: <html:text property="cash" value="60"/><html:errors  property="cash"/><br/>
            
</html:form>
<html:submit/><html:cancel/>
</body>
</html>



这个是FORM里的validate()方法:
public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        // TODO Auto-generated method stub
        ActionErrors errors = super.validate(mapping, request);
        
        if (errors == null) {
        System.out.println("errors:" + errors);
        errors = new ActionErrors();
        }
        return errors;

    }


对应的Action:


    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        SetVariablessForm setVariablessForm = (SetVariablessForm) form;// TODO Auto-generated method stub
        VariablesDAO v=new VariablesDAO();
        Variables variable=new Variables();
        variabledeal vupdate=new variabledeal();
        Transaction tran=v.getSession().beginTransaction();
        
        Integer v1=Integer.parseInt(setVariablessForm.getCash());
        //初始现金
        List<Variables> results=v.findByInfor("cash");
        for(Variables vs:results)
        {
            if(v1>0l){
            variable.setNum(v1);
            vupdate.update(variable);
                    }
        }

    
        tran.commit();
        return null;
    }
}


3 楼


[color=00FF00][size=6][size=4]在填入h时(也就是不合法时) ,没有反应,需要在哪里加上message还是别的什么么??[/size][/size][/color]

我来回复

您尚未登录,请登录后再回复。点此登录或注册