Java Wicket:如何更改标签';文本区域';什么事?

Java Wicket:如何更改标签';文本区域';什么事?,java,ajax,model,wicket,Java,Ajax,Model,Wicket,如何在textarea的onkeyup上更改标签的文本?我尝试过这个,但不起作用: Form form; TextArea ta; MyLabel resultDiv; /** * Constructor that is invoked when page is invoked without a session. */ public HomePage(final PageParameters parameters) { this.form = new Fo

如何在textarea的onkeyup上更改标签的文本?我尝试过这个,但不起作用:

Form form;
TextArea ta;
MyLabel resultDiv;


  /**
   * Constructor that is invoked when page is invoked without a session.
   */
  public HomePage(final PageParameters parameters) {

      this.form = new Form("form");
      this.ta = new TextArea("text");
      this.resultDiv = new MyLabel("result");

      this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
        protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
        }
      } );


      form.add( ta );
      form.add( resultDiv );
      add( form );

  }// const

  public class MyLabel extends Label {
    private String text = "original";
    public String getText() {      return text;    }
    public void setText( String text ) {      this.text = text;    }
    public MyLabel( String id ) {
      super( id );
      this.setModel( new PropertyModel(this,"text") );
    }
  }
解决方案

莱昂尼德夫就快到了。生成的代码是:

Form form;
TextArea ta;
Label resultDiv = new Label( "result", new PropertyModel(this,"labelText") ){
  { setOutputMarkupId( true ); }
};

private String labelText = "original";


/**
 * Constructor that is invoked when page is invoked without a session.
 */
public HomePage(final PageParameters parameters) {

    this.form = new Form("form");

    this.ta = new TextArea("text");
    this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
      protected void onEvent( AjaxRequestTarget target ) {
        System.out.println( "Ajax!" );
        labelText = "Foobar";  // Doesn't even need get/set, which is great.
        target.addComponent( resultDiv );
        //resultDiv.renderComponent(); // WRONG!!
      }
    } );

    form.add( ta );
    form.add( resultDiv );
    add( form );

}// const
最后一个问题是我在添加
renderComponent()
时的错误直觉——出于某种原因,它使标签保持不变

顺便说一句,结果很快就会成为沙箱


谢谢你的帮助

而不是resultDiv.renderComponent();
请尝试resultDiv.modelChanged()

如果您想在AJAX事件后更新组件,您必须做两件事:

  • 可更新组件必须具有setted flag
    setOutputMarkupId==true
  • 必须将此组件添加到目标OneEvent方法

    this.resultDiv.setMarkupOutputId(true);
    
    protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          //resultDiv.setModel(  );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
          target.add(resultDiv);
    }
    

  • PS我不理解你代码的很多部分

    >我不理解您代码的许多部分。-我都不知道:)我是Wicket的新手,它并不完全直观。Wicket非常棒的特性是将字段绑定到组件。所以,您不需要“公共类MyLabel扩展标签”。创建PropertyModel:MyPage.时需要全部。。{String text=“original”;…在构造函数中…在这个wicket绑定标签和文本字段之后添加新标签(“id”,new PropertyModel(this,“text”);}。因此,当您处理submit或AJAX事件时,您不必像:text=Label.getValue()System.out.println(text)那样编写smth;wicket会自动写入:sysout(text);您可能对本文感兴趣:最后一个问题-处理事件时如何获取textarea的内容?其模型未更新(仍包含我初始化的内容)。尝试此链接:我认为您可以将content TextArea绑定到其他字段,并在ajax行为中使用labelText=contentText。更有趣的是,尝试将labelText也绑定到TextArea,甚至将Label.getValue绑定到TextArea。不起作用;IMHO
    modelChanged()
    是一个要覆盖的回调(仅从名称猜测)。