【问题标题】:Create Wicket component embedding a Textfield and its FeedbackPanel创建嵌入文本字段及其反馈面板的 Wicket 组件
【发布时间】:2014-08-09 00:54:58
【问题描述】:

我写了这段代码:

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>

我想创建一个能够管理 TextField 及其反馈面板的 Wicket 组件,我该怎么做?

【问题讨论】:

  • 欢迎来到 StackOverflow !在这个网站上,如果你清楚地说出你尝试过的东西是什么不起作用,你会得到更多相关的答案。
  • 谢谢 :) 我尝试使用 Panel、Border,但我无法做我想做的事,所以我正在寻找正确的方法,或者至少一个轨道.. .
  • 为什么面板不起作用?
  • 我没有很好地使用 Panel。以@Martin 为例,我找到了正确的做法。

标签: java components wicket


【解决方案1】:

查找课程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 像面板一样工作,因此您还需要为此类添加自己的 html 文件。然后您可以将此组件添加到表单中。

form.add(new FeedbackTextField<String>("input1", Model.of("")));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多