JTextfield をテキスト検証にバインドしてから、それを pojo モデルにバインドしようとしています。私の目標は、ユーザーが特定のテキスト長で許容される特定の文字セットを入力できるようにし、バインディングを使用してモデルにテキストを設定できるようにすることです。コード スニペットを以下に示します。
パブリック クラス TestValidationBinding {
プライベート JTextField フィールド。
プライベート ModelVo モデルVo;
public TestValidationBinding() {
フィールド = 新しい JTextField();
modelVo = 新しい ModelVo();
field.setDocument(new PlainDocument() {
private static final long serialVersionUID = 1L;
@オーバーライド
public void insertString(int offs, String str, AttributeSet a)
BadLocationException をスローします {
// キー入力のその他の検証、長さのチェック
整数制限 = 15;
if (str == null) {
戻る;
}
if ((getLength() + str.length()) <= limit) {
super.insertString(offs, str, a);
}
}
});
プロパティ srcProperty = BeanProperty.create("テキスト");
プロパティ tgtProperty = BeanProperty.create("テキスト");
AutoBinding バインディング = Bindings
.createAutoBinding(UpdateStrategy.READ_WRITE、フィールド、
srcProperty、modelVo、tgtProperty);
binding.bind();
}
}
ModelVO クラスは次のとおりです。
公開クラス ModelVo {
プライベート文字列テキスト。
パブリック文字列 getText() {
テキストを返します。
}
public void setText(文字列テキスト) {
System.out.println("テキストは:" + テキスト);
this.text = テキスト;
}
}
ModalVO クラスで必要なプロパティの変更を実行するために AspectJ を使用しています。(これを達成するためにこのリンクをたどりました:: http://yakafokon.wordpress.com/2008/12/02/beans-binding-jsr-295-with-pojo-and-aop/#comments )。
さて、私の問題は、バインディングを使用しない場合、検証は適切に行われますが、テキストがモーダルに設定されないことです。しかし、テキストフィールドをバインドすると、テキストはモデルに正しく設定されますが、検証は機能しません。検証とバインディングの両方を一緒に使用しているときに機能しない理由を誰かが洞察できますか?