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

Play2はPlay1とは異なる動作をします。

その式は書き直されません:

anInstance.field = newValue;

したがって、引数を検証するには、セッターを直接呼び出す必要があります。

ソース:https ://groups.google.com/forum/#!topic / play-framework / S8DHAcYHh-A

于 2012-10-06T22:17:47.417 に答える