【问题标题】:How to validate a list in Struts 2 with a condition? (Validation Expressions)如何使用条件验证 Struts 2 中的列表? (验证表达式)
【发布时间】:2014-06-18 14:22:58
【问题描述】:

使用表达式,我试图确保列表中至少有一个对象(不为空)或复选框被选中为真...我尝试过的任何方法似乎都不起作用。

在我的验证 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:

动作类的相关sn-ps:

   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 表达式验证器。似乎我无法在表达式中正确引用这个列表。

【问题讨论】:

  • 区别是一个是字段表达式,一个是表达式

标签: xml validation struts2 expression ognl


【解决方案1】:

只要您使用列表,请尝试以下表达式。 length 用于数组。 not in 在您的情况下总是返回 true,这就是第二个表达式有效的原​​因。

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

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

【讨论】:

  • 谢谢,我试试看。
  • 好的,我都试了一次,但它仍然给我带来问题(它不会触发错误)。我将更新我的问题并添加我的 JSP。
  • 为什么你的类是静态的?是内部类吗?
  • 是的,该类存在于我的操作类中。
  • 您必须在单独的文件中使其成为非静态公共类。
猜你喜欢
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多