通常不用搞得这么复杂的,通过某个form值在重载validate方法时判断一下再抛自定义错误即可。把可能的情况用程序来判断。ActionErrors errors = super.validate(mapping, request);Validator validator = new Validator(resources, key)因为是依据mapping来寻找配置项的,而且是根据指定资源new出来的。因此你多写几个配置也行,在程序中更换你的mapping,例如path为
/action1
那么你在validate里可以配置成:
/action1_test1
/action1_test2
/action1_test3
然后在validate判断选择之。如果觉得还是复杂,我也想不出什么好办法。

解决方案 »

  1.   

    我已经实现了,前台用自己的tag,所有配置信息放在数据库中
    不用XML了,所有服务器端验证直接调用common validation 的API
    其实struts自己也是裁剪common validation来做的,不过它把配置文件用成xml的
    common validation 本身并不提供这种配置了,所以本质上可以在
    任何框架中使用common validation framework
    =========================================================================
    谢谢上面两位的回帖!
      

  2.   

    // set the dynamic value to formbean
    BeanUtils.setProperty(form, "fieldProperties", fieldProperties);
    LoadValidatorResource vResource = new LoadValidatorResource(fieldIdArr);
    Validator validator = new Validator(vResource.getResources(), "XXForm");
    validator.setParameter(Validator.BEAN_PARAM, form);
    ActionMessages errorsArray = new ActionMessages();
    validator.setParameter("javax.servlet.http.HttpServletRequest",request);
    validator.setParameter("org.apache.struts.action.ActionMessages", errorsArray);

    // validation
    ValidatorResults vResults = validator.validate();
      

  3.   

    LoadValidatorResource.java
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    /**
      * <p> 
     * this method will get relevant property file and read properties files
     * </p>
     * @return Nil
     */
    protected void loadPathnames() {
    // Set a default just in case
    String paths = "validation-rules.xml";
    InputStream stream = null;
    try { // Load some properties file
    stream = this.getClass().getResourceAsStream(
    "/validator.properties"); if (stream == null) {
    stream = this.getClass().getClassLoader().getResourceAsStream(
    "validator.properties");
    System.out.println("\t PROCESSING: Class Loader way to get the Resource ");
    } else {
    System.out.println("\t PROCESSING: Class way to get the Resource ");
    } // Get the pathnames string from the properties file
    if (stream != null) {
    Properties props = new Properties();
    props.load(stream);
    paths = props.getProperty("validator-pathnames");
    //System.out.println(paths);
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    setPathnames(paths);
    } /**
     * <p> this method will initilize validation resource </p>
     * @throws IOException
     */
    protected void initResources(String[] fieldIdArr) throws Exception {

    try {
    if (getPathnames() != null && getPathnames().length() > 0) {
    String pathStr = getPathnames();

    InputStream[] in = new InputStream[2];

    // System.out.println("validation file  = " + pathStr);
    in[0] = this.getClass().getResourceAsStream(pathStr);
    in[1] = generateValidationXmlFile(fieldIdArr);
    if (in[0] == null || in[1] == null ) {
    System.out.println("\t PROCESSING: can't find the resource files ");
    }

    resources = new ValidatorResources(in);
    resources.process();
    // log.debug("\t PROCESSING: generated resource content");
    // System.out.println(" resource initilization "); }
    } catch (SAXException se) {
    se.printStackTrace();
    }

    }