单击按钮后,Wicket添加方法

时间:2012-10-05 17:30:19

标签: java wicket

我有问题,按钮点击后如何“重新加载”添加方法?例如:

ArrayList list = new ArrayList();
DataView dataView = new DataView("listview", new ListDataProvider(list)) {
    public void populateItem(final Item item) {
            final DBUser user = (DBUser) item.getModelObject();
            item.add(new Label("id", String.valueOf(user.getid())));
     }
};
final Load load = new Load(dataView);
    Form<?> form2 = new Form<Void>("form2") {
        @Override
        protected void onSubmit() {

            load.tick();
        }
};
add(dataView);

现在加载类:

public class Load {
private DataView dataView;

public Load(DataView dataView)
{
    this.dataView = dataView;
}

public DataView tick()
{
    Session session = HibernateUtil.getSessionFactory().openSession();//getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery(
            "from DBUser");
    ArrayList list = new ArrayList();
    for (Iterator it=query.iterate();it.hasNext();) {
        DBUser row = (DBUser) it.next();
        DBUser dbuser = new DBUser(row.getid());
        list.add(dbuser);
    }

    dataView = new DataView("listview", new ListDataProvider(list)) {
        public void populateItem(final Item item) {
            final DBUser user = (DBUser) item.getModelObject();
            item.add(new Label("id", String.valueOf(user.getid())));
        }
    };
    session.close();
    return dataView;
}

}

现在添加(line)方法只调用一次,如何在每次点击按钮后调用它?

这是我的.html

<table frame="below" border=1>
    <tr wicket:id="listview">
         <td align="center" width="40"><span wicket:id="id"></span></td>
     </tr>
 </table>

1 个答案:

答案 0 :(得分:2)

我想,您可以通过以下方式重新加载DataView:

Form<?> form2 = new Form<Void>("form2") {

    @Override
    protected void onSubmit() {
        StartPage.this.replace( load.tick() );
        // or you can use 
        // StartPage.this.addOrReplace( load.tick() ) 
        // instead
    }
};

但是你的代码也需要严格的重构!至于我,我会这样写,以提高可读性:

final Load load = new Load();

Form<?> form2 = new Form<Void>("form2") {
    @Override
    protected void onSubmit() {
        StartPage.this.replace( createDataView( load.tick() ) );
    }
};
add(form2);

add( createDataView( new ArrayList() ) );

createDataView(list)方法的实现:

private DataView createDataView(List list) {
    return new DataView("listview", new ListDataProvider(list)) {
        public void populateItem(final Item item) {
            final DBUser user = (DBUser) item.getModelObject();
            item.add(new Label("id", String.valueOf(user.getid())));
        }
    };
}

Load类的实现:

public class Load {

    public List tick() {
        List result = new ArrayList();
        // ...
        // populate list from database
        // ...
        return result;
    }
}

希望这有帮助。