我正在尝试获取数据&使用组件标签
显示该数据
但是这些数据没有出现在页面上
所有标签都在表格和行中。行在此处递增但行未获得标签
package com.cerebrum.pages;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import com.cerebrum.common.Home;
import com.cerebrum.hibernate.AddForumSubCategoryEntity;
import com.cerebrum.hibernate.ForumHome;
import com.cerebrum.pojo.ForumModel;
public class Forum extends Home
{
ForumHome forumHome=new ForumHome();
ForumModel forumModel=new ForumModel();
List<ForumModel> listForum=new ArrayList<ForumModel>();
public Forum()
{
super();
add(new ForumForm());
}
class ForumForm extends Form
{
public ForumForm()
{
super("ForumForm");
setModel(new CompoundPropertyModel(forumModel));
List<AddForumSubCategoryEntity> list=forumHome.getAll();
for(AddForumSubCategoryEntity addForumSubCategoryEntity:list)
{
listForum.add(new
ForumModel(addForumSubCategoryEntity.getMain_key(),
addForumSubCategoryEntity.getDescription()));
}
ListView listView=new ListView("listForum",listForum)
{
@Override
protected void populateItem(ListItem item)
{
ForumModel model=(ForumModel)item.getDefaultModelObject();
Label lblMainCategory=new Label("lblMainCategory",new
PropertyModel(model, "lblMainCategory"));
item.add(lblMainCategory);
Label lblLastSubCategory=new
Label("lblLastSubCategory",new PropertyModel(model, "lblLastSubCategory"));
item.add(lblLastSubCategory);
Label lblTotalNoofPost=new Label("lblTotalNoofPost",new
PropertyModel(model, "lblTotalNoofPost"));
item.add(lblTotalNoofPost);
}
};
listView.setOutputMarkupId(true);
add(listView);
}
}
}
答案 0 :(得分:1)
尽量避免创建这个中间列表“listForum”如果你的forumModel有一个方法“getListForum”会更好,所以你不需要将模型传递给ListView。 (请参阅CompoundPropertyModels如何在此工作https://cwiki.apache.org/WICKET/working-with-wicket-models.html)。
在ListView中你使用的是“getDefaultModelObject()”而不是“getModel”,然后你将它用作PropertyModel的模型,这很奇怪。
我不完全理解您的模型(ForumHome或ForumModel是否实现了IModel?),但我想这样的事情会更好:
public class Forum extends Home {
private ForumHome forumHome = new ForumHome();
private ForumModel forumModel = new ForumModel( forumHome );
public Forum() {
super();
add(new ForumForm("ForumForm", forumModel));
}
private static class ForumForm extends Form {
public ForumForm(String wicketId, ForumModel forumModel) {
super(wicketId, new CompoundPropertyModel(forumModel));
ListView<ForumModel> listView = new ListView<ForumModel>("listForum") {
@Override
protected void populateItem(ListItem item) {
IModel<ForumModel> model = item.getModel();
item.add( new Label("lblMainCategory", new PropertyModel(model, "lblMainCategory")) );
item.add( new Label("lblLastSubCategory", new PropertyModel(model, "lblLastSubCategory")) );
item.add( new Label("lblTotalNoofPost", new PropertyModel(model, "lblTotalNoofPost")));
}
};
listView.setOutputMarkupId(true);
add(listView);
}
}
}