问题
I am trying to take data & display that data by using component Label
but that data does not seen onto page
all labels are in table-row & row increses here but row doesnt get label
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);
}
}
}
回答1:
Try to avoid creating this intermediate list "listForum" it would be better if your forumModel have a method "getListForum" so you don't need to pass the model to the ListView. (see how CompoundPropertyModels works here https://cwiki.apache.org/WICKET/working-with-wicket-models.html).
And inside your ListView you are using "getDefaultModelObject()" instead of "getModel" and then you are using this as a model for the PropertyModel, this is strange.
I don't fully understand your model (Are ForumHome or ForumModel implementing IModel?), but I guess that something like this would be a better approach:
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);
}
}
}
来源:https://stackoverflow.com/questions/9850232/data-comes-from-list-but-does-not-seen-onto-the-page