验证表单是否已更改

Mit*_*tch 8 java spring spring-webflow

我正在编写一个验证器,需要测试弹簧形式对象是否已更改.

在验证程序中,如果未对表单进行任何更改,则应显示错误.

有弹簧机制吗?

这是因为我提交时会进行非常昂贵的Web服务更新调用,如果没有进行任何更改,我需要阻止进行webservice调用.

干杯.

cli*_*unk 1

重写 hashCode() 函数以确保当表单值更改时返回不同的值。保存函数的返回值并测试它是否改变。

public class Person {
  String first;
  String last;
  int prevHash;
  @Override
  public int hashCode() {
    // does not include the value of currHash
    String stringHash = String.format("%1$-29s%2$-29s", first,last);
    return stringHash.hashCode();
  }
}

public class PersonValidator  implements Validator {
  public void validate(Object obj, Errors e) {
    Person p = (Person) obj;
    if (p.hashCode() == p.getPrevHash()) {
      e.reject("person", "unchanged");
    } 
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

我不认为 Spring 提供了验证测试来检查表单支持对象是否已更改

请注意,您可以在允许用户提交表单之前在客户端执行其他测试。