在 Wicket 的构造函数中调用 getString() 会出现错误
我在检票口本地化方面遇到一些问题。
这是代码:
private String displayString;
private TextField<String> myTextField;
public myPage(DomainObject domainObject){
if(domainObject != null)
displayString = domainObject.getDisplayString();
myTextField = new TextField<String>("myTextField", new PropertyModel<String>(this, "displayString"));
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- error message here
}
问题是在构造函数中调用 getString 会导致错误消息(“...这有时会导致返回无效或没有本地化资源...”)。 我想对 TextField 使用 PropertyModel,因为我不想翻译从 domainObject.getDisplayString() 获得的字符串。我不希望 TextField 中所做的更改直接影响 domainObject 中的值。 通过这样做而不是 getString 可以消除错误消息:
if(Strings.isEmpty(displayString))
displayString = new ResourceModel("mandatory").getObject(); //<- no error message
据我了解,这与调用 getString 是一样的(您只需消除警告,但问题仍然存在)。 我想到的解决方案是这样的:
@Override
protected void onAfterRender() {
super.onAfterRender();
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- no error message
}
有人看到这个解决方案有问题吗?也许我的想法还不够“wickety”?
I'm having some problems with localization in wicket.
This is the code:
private String displayString;
private TextField<String> myTextField;
public myPage(DomainObject domainObject){
if(domainObject != null)
displayString = domainObject.getDisplayString();
myTextField = new TextField<String>("myTextField", new PropertyModel<String>(this, "displayString"));
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- error message here
}
The problem is that calling getString in the constructor results in an error message("...This can sometimes lead to an invalid or no localized resource returned...").
I want to use a PropertyModel for the TextField since I don't want to translate the string I get from domainObject.getDisplayString(). I don't want the changes made in the TextField to affect the value in domainObject directly.
It's possible to get rid of the error message by doing this instead of getString:
if(Strings.isEmpty(displayString))
displayString = new ResourceModel("mandatory").getObject(); //<- no error message
To my understanding, this is the same thing as calling getString (you just hack away the warnings, but the problem still exist).
A solution i thought of is this:
@Override
protected void onAfterRender() {
super.onAfterRender();
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- no error message
}
Does anyone see a problem with this solution? Maybe I'm not thinking "wickety" enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用 getString() 要求组件位于组件层次结构内,在该层次结构中它可以访问其父级,以便有机会回退到那里定义的属性或树中更高层定义的属性。这在组件的构造函数中是不可能的(因为您稍后将其添加到其父级)。 Wicket 1.5 引入了 onInitialize这些操作的函数。对于在此之前的 Wicket 版本,有一种简单的方法可以模拟此行为:
在您的基础组件和页面中定义一个非最终空方法 as
并将其添加到 onBeforeRender 方法中:
然后您可以在中使用重写的 onInitialize() 方法任何组件处理必须等到组件层次结构建立之后才能处理的事情。
Calling getString() requires the component to be inside a component hierarchy, where it can access it's parent to have the chance to fall back to properties defined there or further up in the tree. This isn't possible inside the component's constructor (as you add it to it's parent at a later point). Wicket 1.5 introduces the onInitialize function for these operations. With Wicket versions prior to this, there is an easy way to emulate this behaviour:
In your base component and page define a non-final empty method as
and add this to the onBeforeRender method:
Then you can use an overridden onInitialize() method in any of your components to deal with stuff that has to wait until the component hierarchy is established.
可重用行为怎么样:
不过,您必须在验证器中检查提交的值。
HTML5 占位符甚至更好。
What about a reusable behavior:
You'd have to check submitted values in a validator though.
HTML5 placeholders are even nicer.