简体   繁体   English

创建嵌入文本字段及其FeedbackPanel的Wicket组件

[英]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.

相关问题 Wicket AjaxButton不会更新FeedbackPanel - Wicket AjaxButton does not update FeedbackPanel 在wicket中,如何从面板中的ajax组件的onSubmit刷新反馈面板? - In wicket, how can you refresh a feedbackpanel from an onSubmit of an ajax component within a panel? 小票:即使“反馈面板”在页面上,“针对组件的反馈消息也未呈现” - Wicket: “Component-targetted feedback message was left unrendered” even though the FeedbackPanel is on the page 触发检票口反馈面板时,如何自动向上滚动检票口面板? - How to automatically scroll up a wicket panel when wicket feedbackpanel is trigged? 如何在Wicket FeedbackPanel的info()/ error()方法上调用Javascript方法? - How to call a Javascript method on info() / error() method of Wicket FeedbackPanel? 如何在无状态Wicket应用程序中使用FeedbackPanel? - How do I use FeedbackPanel in a stateless Wicket application? 在 Wicket 中的有效输入提交上,FeedbackPanel 显示的错误消息未清除 - Error message displayed by FeedbackPanel does not clear on valid input submission in Wicket Java:如何将值从 javascript 设置为检票口组件文本字段 - Java: How to set a value from javascript to a wicket component textfield 小门6:更新组件时TextField光标位置移动 - Wicket 6: TextField cursor position moves when component updated Wicket TextField具有默认值 - Wicket TextField with default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM