首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Playframework Setter封装

Playframework Setter封装
EN

Stack Overflow用户
提问于 2012-10-07 05:17:41
回答 1查看 119关注 0票数 0

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

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

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

下面是代码片段:

代码语言:javascript
复制
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));
  }

}

我是不是漏掉了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-07 06:17:47

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

它不会重写表达式:

代码语言:javascript
复制
anInstance.field = newValue;

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12763959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档