【问题标题】:Wicket: Changing the text of an AjaxButton on submitWicket:在提交时更改 AjaxButton 的文本
【发布时间】:2011-03-22 17:46:05
【问题描述】:

我是 Wicket 的菜鸟,并试图在提交时更改 AjaxButton 的文本。所以想法是页面第一次加载时,用户会看到一个标记为例如的 AjaxButton。 “1”,点击按钮后,按钮的标签变为“2”,下一次点击后变为“3”,依此类推......这不难,但正如我所说,我是一个noobie 说到检票口。感谢所有帮助!

form.add(new AjaxButton("ajax-button", form)
    {
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form)
        { //how to change Button label here?
         }

}

【问题讨论】:

    标签: java ajax button wicket ria


    【解决方案1】:

    答案很简单:使用模型。

            //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);
    
            }
        });
    

    这可能是 Wicket 最重要的规则:当您需要更改某些内容时,请使用模型。这需要一些时间来适应,特别是如果您有更多“传统”框架的经验,并且也没有使用过 Swing。

    注意:将计数器保留在页面类中可能不是一个好主意,但总体思路是一样的。

    【讨论】:

    • 我花了一些时间试图让这个独立按钮(&lt;input type="button"&gt; & AjaxButton 或 AjaxLink)工作,它不是任何表单的一部分。奇怪的是,只有在我在按钮周围添加了 HTML 和 Java 中的(虚拟)表单后,它才会起作用。以防万一其他人也对此感到疑惑。检票口 1.4.
    • 这里的例子中括号是不是放错了?表单应该作为参数发送到 AjaxButton 而不是 PropertyModel。
    【解决方案2】:

    除了 biziclop 的回答,这里是一个改变参数的文本的解决方案。

    在您的 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);
    

    在您的 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>
    

    最后,您的本地化文件将包含如下一行:

    IdForLocalizerInYourLocalizerFile= The variable's value is {0}. It will be replaced whenever it changes and button component is added to target. Text will remain.
    

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 2018-09-02
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 2020-09-20
      • 2014-05-03
      • 2012-08-12
      相关资源
      最近更新 更多