Wicket - Reusable panels with java inheritance

青春壹個敷衍的年華 提交于 2019-12-23 08:51:57

问题


  1. I have following java classes:

  2. I have a two form components which are mapped to Type1Task and Type2Task with a CompoundPropertyModel:

Problem: Duplicate Code. I want the D and E Fields to map to a task, not the task types. Is there any way to make this reusable when I hava java inheritance?

If yes, can u provide me with tutorials or references?

Additional info: The task can contain a timer(thread). If you click on submit button the timer will be started.

Thank you!


回答1:


You can duplicate the same class hierarchy in the ui.

public class TaskFormPanel<T extends Task> extends Panel {

public TaskFormPanel(String id, IModel<T> model)
     super(id, new CompoundPropertyModel(model));
     add(new TextField("d"));
     add(new TextField("e));
     add(new Button("submit) {
           (...)
     }
}

}


public class Task1FormPanel extends TaskFormPanel<Task1> {

public TaskFormPanel(String id, IModel<Task1> model)
     super(id, model);
     add(new TextField("a"));
     add(new TextField("b));
     add(new TextField("c"));
}

}


public class Task2FormPanel extends TaskFormPanel<Task2> {

public TaskFormPanel(String id, IModel<Task1> model)
     super(id, model);
     add(new TextField("x"));
     add(new TextField("y));
     add(new TextField("z"));
}

}

And the HTML files:

TaskFormPanel:

<wicket:panel> 
   <wicket:child/>
   <label>d</label> <input wicket:id="d">
   <label>e</label> <input wicket:id="e">
   <input type="submit" wicket:id="submit"/>
</wicket:panel>

Task1Panel.html:

<wicket:extend>
   <label>a</label> <input wicket:id="a">
   <label>b</label> <input wicket:id="b">
   <label>c</label> <input wicket:id="c">
</wicket:extend>

Task2Panel.html:

<wicket:extend>
   <label>x</label> <input wicket:id="x">
   <label>y</label> <input wicket:id="y">
   <label>z</label> <input wicket:id="z">
</wicket:extend>

Note: make sure you use some loadabledetachablemodel to wrap the task objects in if they contain a reference to a thread otherwise you will run into serialization issues. A singleton registry that stores your tasks and can return them by some random key is enough.




回答2:


public class TaskPanel extends FormComponentPanel{

      private IModel<Task> taskModel;

      public TaskPanel(String id, IModel<Task> model){
      taskModel = model;
      add(new TextField("DTextField", new PropertyModel(taskModel, "D")));
      add(new TextField("ETextField", new PropertyModel(taskModel, "E")));
      }
}

Is this what you wanted, or didn't I understand correctly?



来源:https://stackoverflow.com/questions/14506698/wicket-reusable-panels-with-java-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!