Playframework Setter Encapsulation



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

当我在我的示例应用程序设置器上尝试同样的事情时,没有调用验证。

我在调试模式下运行应用程序,并在产品类的 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));
  }
}

我在这里错过了什么吗?

重头戏 2 的行为与重头戏 1 不同。

它不会重写该表达式:

anInstance.field = newValue;

因此,您必须直接调用 setter 来验证参数。

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

最新更新