[英]Create Wicket component embedding a Textfield and its FeedbackPanel
我寫這段代碼:
Java文件:
TextField<String> input1 = (TextField<String>) new TextField<>("input1", Model.of("")).add(new UrlValidator());
TextField<String> input2 = (TextField<String>) new TextField<>("input2", Model.of("")).add(new UrlValidator());
add(new Form<>("form").add(
new FeedbackPanel("feedbackInput1").setFilter(new ComponentFeedbackMessageFilter(input1)),
new FeedbackPanel("feedbackInput2").setFilter(new ComponentFeedbackMessageFilter(input2)),
input1,
input2
));
模板文件:
<form wicket:id="form">
<!-- first -->
<div>
<wicket:panel wicket:id="feedbackInput1" />
<label for="input1">Input 1:
<input type="text" wicket:id="input1" id="input1" />
</label>
</div>
<!-- second -->
<div>
<wicket:panel wicket:id="feedbackInput2" />
<label for="input2">Input 2:
<input type="text" wicket:id="input2" id="input2" />
</label>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
為了避免冗余,我想創建一個組件(例如:TextFieldWithFeedback),可以這樣使用:
Java文件:
TextField<String> input1 = (TextField<String>) new TextField<>("input1", Model.of("")).add(new UrlValidator());
TextField<String> input2 = (TextField<String>) new TextField<>("input2", Model.of("")).add(new UrlValidator());
add(new Form<>("form").add(
new TextFieldWithFeedback("input1", "Input 1: ", input1),
new TextFieldWithFeedback("input2", "Input 2: ", input2)
));
模板文件:
<form wicket:id="form">
<!-- first -->
<div wicket:id="input1" />
<!-- second -->
<div wicket:id="input2" />
</form>
我想創建一個Wicket組件,該組件可以管理TextField及其feedbackPanel,該怎么辦?
查找類FormComponent 。 您可以執行以下操作:
public class FeedbackTextField<T> extends FormComponent<T> {
public FeedbackTextField(String id) {
this(id, null);
}
public FeedbackTextField(String id, IModel<T> model) {
super(id, model);
TextField<T> tf = new TextField<T>("tx");
add(tf);
add(new FeedbackPanel("fb").setFilter(new ComponentFeedbackMessageFilter(tf)));
}
}
FormComponent的作用類似於Panel,因此您還需要為此類添加自己的html文件。 然后,您可以將此組件添加到表單。
form.add(new FeedbackTextField<String>("input1", Model.of("")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.