java Wicket:在提交时更改 AjaxButton 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5395603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wicket: Changing the text of an AjaxButton on submit
提问by user594883
I'm a noob to Wicket and trying to change the text of a AjaxButton on submit. So the idea is that the for the first time the page loads, the user sees an AjaxButton labeled e.g. "1", after clicking the button, the label of the button changes to "2" and after the next click to "3" and so on...This can't be hard, but as I said, I'm a noobie when it comes to wicket. All help appreciated!
我是 Wicket 的菜鸟,并试图在提交时更改 AjaxButton 的文本。所以这个想法是第一次加载页面时,用户看到一个标记为“1”的 AjaxButton,单击按钮后,按钮的标签变为“2”,下次单击后变为“3”和等等......这并不难,但正如我所说,当谈到检票口时,我是个菜鸟。感谢所有帮助!
form.add(new AjaxButton("ajax-button", form)
{
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form)
{ //how to change Button label here?
}
}
}
回答by biziclop
The answer is simple: use a model.
答案很简单:使用模型。
//counter field declared in page class
private int counter;
...
form.add(new AjaxButton("ajax-button", new PropertyModel<String>(this,
"counter", form)) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
counter++;
target.addComponent(this);
}
});
This is probably the most important rule of Wicket: when you need something changing, use a model. This takes some time getting used to, especially if you have experience with more "traditional" frameworks, and haven't used Swing either.
这可能是 Wicket 最重要的规则:当您需要更改某些内容时,请使用模型。这需要一些时间来适应,特别是如果您有使用更多“传统”框架的经验,并且也没有使用过 Swing。
N.b.: keeping the counter in your page class may not be a good idea, but the general idea is the same.
注意:将计数器保留在您的页面类中可能不是一个好主意,但总体思路是相同的。
回答by BlondCode
Additionally to biziclop's answer, here is a solution for text with changing parameter.
除了 biziclop 的回答之外,这里是具有更改参数的文本的解决方案。
In your java code:
在您的 Java 代码中:
AjaxButton yourButton = new AjaxButton("btnId"){
//your button's implementation goes here
};
int yourVariable = 42;
Label yourLabel = new Label("labelId", new Model<String>() {
public String getObject() {
String text = MessageFormat.format(new Localizer().getString("IdForLocalizerInYourLocalizerFile", null), yourVariable);
return text;
}
})
yourButton.add(yourLabel);
In your html:
在你的 html 中:
<a type="submit" wicket:id="btnId">
<span wicket:id="labelId">[This text will never be seen, will be replaced by "The var..."]</span>
</a>
Finally your localization file will contain a line like:
最后,您的本地化文件将包含如下一行:
IdForLocalizerInYourLocalizerFile= The variable's value is {0}. It will be replaced whenever it changes and button component is added to target. Text will remain.