Wicket PropertyModel 奇怪吗?
我是 Wicket 新手,正在尝试以下配置:
class User {
private String password;
...
public void setPassword(String password) {
this.password = MD5.encode(password);
}
...
}
尝试使用以下内容绑定到密码并发现 PropertyModel 的默认实现默认绑定到字段,而不是属性(奇怪的名称是吗? )
add(new PasswordTextField("password", new PropertyModel(user, "password"));
他们到底为什么要这样实施? 是否有默认使用 getter 和 setter 的 PropertyModel 替代方案?
谢谢你?
I'm new to Wicket and was trying the following configuration:
class User {
private String password;
...
public void setPassword(String password) {
this.password = MD5.encode(password);
}
...
}
After trying to use the following to bind to the password and finding out that the default implementation of PropertyModel is by default to bound to the field, not the property (weird name eh?)
add(new PasswordTextField("password", new PropertyModel(user, "password"));
Why in the world would they have implemented it this way? And is there a PropertyModel alternative that uses getter and setters by default?
Thank you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PropertyModel
已经可以完成您想要的操作。 当查询PropertyModel
的值时,它会在两个位置查找:如果给定属性存在“getter”方法,
PropertyModel
会调用该 getter检索属性的值。 具体来说,PropertyModel
查找名为get
的方法,其中
是传递给的属性表达式PropertyModel
构造函数,并使用反射调用该方法(如果存在)。如果不存在“getter”方法,
PropertyModel
直接返回属性字段的值。 具体来说,PropertyModel
使用反射查找与传递给PropertyModel
构造函数的属性表达式相匹配的字段。 如果找到匹配的字段,PropertyModel
将返回该字段的值。 请注意,除了公共字段之外,PropertyModel
还会检查私有字段和受保护字段是否匹配。在您的情况下,
PropertyModel
构造函数中使用的属性表达式是"password"
,因此PropertyModel
将首先在PropertyModel
上查找方法code>user 对象称为getPassword
。 如果不存在这样的方法,PropertyModel
将返回私有password
字段的值。由于在您的情况下,
PropertyModel
返回私有字段的值而不是调用“getter”,因此您很可能在User
类中错误地输入了 getter 的名称。 例如,如果您不小心输入了getPasssword
(包含 3 个 s),则PropertyModel
将找不到它,并且会回退到返回私有字段。编辑
如果您不喜欢
PropertyModel
的默认行为,您可以创建PropertyModel
的子类,这将阻止Wicket尝试读取/写入私有字段。 这样,您可以强制通过 getter 和 setter 进行所有属性访问。我编写了一个示例 BeanPropertyModel 类来演示这一点:
PropertyModel
will do what you want already. When aPropertyModel
is queried for its value, it looks in two places:If a "getter" method exists for the given property, the
PropertyModel
calls the getter to retrieve the property's value. Specifically, thePropertyModel
looks for a method namedget<Property>
, where<Property>
is the property expression passed to thePropertyModel
constructor, and calls the method using reflection if it exists.If no "getter" method exists, the
PropertyModel
returns the value of the property field directly. Specifically, thePropertyModel
uses reflection find a field that matches the property expression passed to thePropertyModel
constructor. If a matching field is found, thePropertyModel
returns the field's value. Note that thePropertyModel
will check private and protected fields in addition to public fields for a match.In your case, the property expression used in the
PropertyModel
constructor is"password"
, so thePropertyModel
will first look for a method on theuser
object calledgetPassword
. If no such method exists, thePropertyModel
will return the value of the privatepassword
field instead.Since in your case the
PropertyModel
is returning the private field's value instead of calling the "getter", you most likely mistyped the name of the getter in yourUser
class. For example, f you accidentally typedgetPasssword
(with 3 s's), thePropertyModel
won't find it, and will fallback to returning the private field.EDIT
If you don't like
PropertyModel
's default behavior, you can create a subclass ofPropertyModel
that will prevent Wicket from trying to read/write to private fields. This way, you can force all property accesses to occur through getters and setters.I wrote an example
BeanPropertyModel
class to demonstrate this:迈克·斯普洛斯的回答很好!
不过,还有一点补充:
在这种情况下我不会使用属性模型。 只需编写
并实现正确的行为即可,这正是您想要的。
great answer by mike spross!
one small addition though:
i'd not use property model in this case. just write
and implement the correct bahavior, which does exactly what you want.