【问题标题】:Struts2 Validation behaving weirdStruts2 验证行为怪异
【发布时间】:2012-09-04 21:19:30
【问题描述】:

这是我的验证方法 -

@Override
  public void validate() {
    errors = new HashMap<String, String>();
    if(StringUtil.isBlank(examCode)){
      errors.put("examCode", "Exam code is required");
    }
    if(StringUtil.isBlank(strPaperType)){
      errors.put("paperType", "Paper Type is required");
    }else{
      paperType = PaperType.getPaperTypeByValue(strPaperType);
      if(paperType == null){
        errors.put("paperType", "A valid Paper Type is required");
      }
      if(paperType.equals(PaperType.PRACTICE)){
        if(topicId ==null){
          errors.put("topicId", "Topic Id is required");
        }
      }
    }
    if(StringUtil.isBlank(instructions)){
      errors.put("instructions", "Paper Instructions are required");
    }
  }

'errors' 是我自己在操作中定义的映射。我没有向“fieldErrors”添加任何错误。如果我调试“fieldErrors”,甚至在输入“验证”方法之前发生的事情,我会看到以下两个条目 -

{"id":["Invalid field value for field \"id\"."],"topicId":["Invalid field value for field \"topicId\"."]}

我不知道它们是从哪里添加的。这是我的 struts conf。

<package name="api" extends="json-default" namespace="/api">
    <action name="paper" class="paperApiAction">
      <result name="json" type="json">
        <param name="root">responseDto</param>
      </result>
      <result name="input" type="json">
        <param name="root">fieldErrors</param>
      </result>
    </action>
  </package>

需要帮助。谢谢

【问题讨论】:

    标签: java validation struts2


    【解决方案1】:

    "conversionError" interceptor 将接受类型转换错误并将它们放入fieldErrors。有些用例更容易将其从堆栈中取出;我不确定这是否是其中之一。

    为什么要复制fieldErrors 地图?即使您只是想在自己的 DTO 中使用地图,为什么不使用现有的验证机制呢?区别很小,而且更灵活一些。然后,您可以将纸张类型验证构建到外部化的业务逻辑中,并简化对其和操作的测试。


    无关,但我发现您的代码由于缺少空格而难以阅读。一个天真的重构:

    @Override
    public void validate() {
        errors = new HashMap<String, String>();
    
        if (StringUtil.isBlank(examCode)) {
            errors.put("examCode", "Exam code is required");
        }
    
        if (StringUtil.isBlank(instructions)) {
          errors.put("instructions", "Paper Instructions are required");
        }
    
        if (StringUtil.isBlank(strPaperType)) {
            errors.put("paperType", "Paper Type is required");
        } else {
            validatePaperType();
        }
    }
    
    public void validatePaperType() {
        paperType = PaperType.getPaperTypeByValue(strPaperType);
        if (paperType == null) {
            errors.put("paperType", "A valid Paper Type is required");
            return;
        }
    
        if (paperType.equals(PaperType.PRACTICE) && (topicId == null)) {
            errors.put("topicId", "Topic Id is required");
        }
    }
    

    【讨论】:

    • 如何从堆栈中删除转换拦截器或修改它。就像如果发生任何转换错误,那么值应该是它们的默认值,而不是将其添加到字段错误中。这样我就知道只有一个地方进行验证。
    • @Shwetanka 不是“conversion”拦截器,而是“conversionError”拦截器。你修改你的拦截器堆栈。
    【解决方案2】:

    您的 idtopicId 类变量似乎是整数,但您正试图将它们设置为字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2011-10-27
      相关资源
      最近更新 更多