如何使用条件验证Struts 2中的列表? (验证表达式)

时间:2014-06-18 14:22:59

标签: xml validation struts2 expression ognl

使用表达式,我试图确保一个列表中至少有一个对象(不是空的)或者复选框被检查为true ...我尝试过的任何东西似乎都没有用。

在我的验证xml文件中,我尝试了以下内容......

这始终返回false,即使列表不为空也会导致错误:

    <validator type="fieldexpression">
       <param name="fieldname">uploads</param>
       <param name="expression">chkAtch eq true or <![CDATA[uploads.length > 0]]></param>
       <message key="validation.atch.present" />
    </validator>

结果与上述相同:

<param name="expression">chkAtch eq true or uploads.length > 0</param>

就像这样:

<param name="expression">chkAtch eq true or uploads not in {null, ""}</param>

此操作始终返回true,但如果存在任何字段错误,则会显示错误消息:

<validator type="expression">
    <param name="expression">chkAtch eq true or uploads.length > 0</param>
    <message key="validation.atch.present" />
</validator>

关于如何确保选中复选框(TRUE)或上传列表是否填充了至少一个对象的任何想法?

谢谢,

编辑:

我的JSP:

    <s:file label="File 1" id="file1" name="uploads[0].upload" /> <s:checkbox name="uploads[0].refFile" />
    <s:file label="File 2" id="file2" name="uploads[1].upload" /> <s:checkbox name="uploads[1].refFile" />

<!-- omitted - labels and cosmetics -->
    <s:checkbox name="chkAtch">&nbsp;
         <!-- label -->
    </s:checkbox>

编辑2:

动作类的相关摘录:

   private Boolean chkAtch;
   private List<LinguisticFile> uploads;

   public String confirmInput() throws Exception {
        if (hasFieldErrors()) {
            LOG.debug("[confirmInput] has field errors");
            return createInput();
        }
   // Irrelevant code omitted
   ArrayList<AtchObj> atchObj = new ArrayList<AtchObj>();
        if (uploads != null) {
            for (int i = 0; i < uploads.size(); i++) {
                if (uploads.get(i).getUpload() == null)
                    continue;
                AtchObj atchmnt = new AtchObj(uploads.get(i)
                        .getUploadFileName(),//
                        uploads.get(i).getUploadContentType(),//
                        uploads.get(i).getUpload(),//
                        uploads.get(i).getRefFile()//
                );
                atchObj.add(atchmnt);
            }
        }
    //Irrelevant code omitted
    return "success";
    }

    // Getters and Setters

    public static class LinguisticFile {
        private File upload;
        private String uploadFileName;
        private String uploadContentType;
        private Boolean refFile;

        public File getUpload() {
            return upload;
        }

        public void setUpload(File file) {
            this.upload = file;
        }

        public String getUploadFileName() {
            return uploadFileName;
        }

        public void setUploadFileName(String fileFileName) {
            this.uploadFileName = fileFileName;
        }

        public String getUploadContentType() {
            return uploadContentType;
        }

        public void setUploadContentType(String fileContentType) {
            this.uploadContentType = fileContentType;
        }

        public Boolean getRefFile() {
            return refFile;
        }

        public void setRefFile(Boolean refFile) {
            this.refFile = refFile;
        }
    }

另一个快速编辑:

我应该注意到其他一切都适用于这个项目,包括其他xml表达式验证器......看起来好像我无法正确引用表达式中的这个列表。

1 个答案:

答案 0 :(得分:0)

就您使用列表而言,请尝试以下表达式。 length用于数组。 <{1}}总是在你的情况下返回true,这就是第二个表达式有效的原​​因。

not in

<param name="expression"><![CDATA[chkAtch eq true or uploads.size > 0]]></param>