[英]Create Wicket component embedding a Textfield and its FeedbackPanel
I write this code: 我写这段代码:
Java file: 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
));
Template file: 模板文件:
<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>
To avoid redundancy, I would like to create a component (eg: TextFieldWithFeedback), I can use like this: 为了避免冗余,我想创建一个组件(例如:TextFieldWithFeedback),可以这样使用:
Java file: 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)
));
Template file: 模板文件:
<form wicket:id="form">
<!-- first -->
<div wicket:id="input1" />
<!-- second -->
<div wicket:id="input2" />
</form>
I want to create a Wicket component able to manage a TextField and its feedbackPanel, how can I do? 我想创建一个Wicket组件,该组件可以管理TextField及其feedbackPanel,该怎么办?
Look for the class FormComponent . 查找类FormComponent 。 You could do something like this: 您可以执行以下操作:
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 works like a Panel so you need to add you own html file for this class also. FormComponent的作用类似于Panel,因此您还需要为此类添加自己的html文件。 Then you can add this component to a form. 然后,您可以将此组件添加到表单。
form.add(new FeedbackTextField<String>("input1", Model.of("")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.