数据来自列表,但没有在页面上看到 [英] Data comes from list but does not seen onto the page

查看:79
本文介绍了数据来自列表,但没有在页面上看到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试收集数据&通过使用组件标签

显示该数据,但该数据不会显示在页面上

所有标签都在表格行&行在这里增加,但行没有标签

  package com.cerebrum.pages; 

import java.util.ArrayList;

import java.util.List;

导入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;
公共类论坛扩展主页
{
论坛首页forumHome = new ForumHome();
ForumModel forumModel = new ForumModel();
列表< ForumModel> listForum = new ArrayList< ForumModel>();
public Forum()
{
super();
add(new ForumForm());
}
类ForumForm扩展Form
{
public ForumForm()
{
super(ForumForm);
setModel(new CompoundPropertyModel(forumModel));

列表< AddForumSubCategoryEntity>列表= 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);




解决方案 https://cwiki.apache.org/WICKET



在ListView中,您使用的是getDefaultModelObject()而不是getModel,然后使用getDefaultModelObject



我没有完全理解你的模型(ForumHome或ForumModel是否实现了IModel?),但是我使用这个模型作为PropertyModel的模型,这很奇怪。猜猜这样的事情会是一个更好的方法:

 公共类论坛扩展主页{

private ForumHome论坛首页= new ForumHome();
private ForumModel forumModel = new ForumModel(forumHome);

public Forum(){
super();

add(新的ForumForm(ForumForm,forumModel));

$ b $ 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);
}
}
}


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);
    }
}
}

解决方案

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);
    }
}
} 

这篇关于数据来自列表,但没有在页面上看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆