0

我正在为 web 应用程序使用 Play 2.0。了解Play的属性模拟:http://www.playframework.org/documentation/1.2.3/model#properties

当我在示例应用程序上尝试相同的操作时,setter 的验证没有被调用。

我在调试模式下重新运行应用程序并将断点放在 Product 类的 Setter 方法上,但它没有被执行。

这是代码片段:

public class Product {

public String name;
public Integer price;

  public void setPrice(Integer price) {
     if (price < 0) {
         throw new IllegalArgumentException("Price can’t be negative!");
     }
     this.price = price;
   }
}    


public class Application extends Controller {

  public static Result index() {
   Product p=new Product();
   p.name="Test";
   p.price=-1; //I am expecting that code will throw IllegalArgumentException but its not

      return ok(index.render(p.name));
  }

}

我在这里错过了什么吗?

4

1 回答 1

1

播放 2 的行为与播放 1 不同。

它不会重写那些表达式:

anInstance.field = newValue;

所以你必须直接调用 setter 来验证参数。

来源:https ://groups.google.com/forum/#!topic/play-framework/S8DHAcYHh-A

于 2012-10-06T22:17:47.417 回答