【问题标题】:JSF - Can't display list of objects from ManagedBeanJSF - 无法显示来自 ManagedBean 的对象列表
【发布时间】:2015-12-09 02:39:26
【问题描述】:

也许有人知道我为什么不能在我的应用中显示类别列表。文件 categories.xhtml 仅显示单词 Categories。类 Category 在构造函数中设置 id 和 name。

categories.xhtml

<ui:component>
    <h:form>
        <h4>Categories</h4>
        <ul>
            <ui:repeat var="category" value="#{categoriesBean.modelCategories}">
                <li><h:outputText value="#{category.name}">
                    </h:outputText>
                     </li>
            </ui:repeat>
        </ul>
    </h:form>
</ui:component>

CategoriesBean.java

    @ManagedBean
    @RequestScoped
    public class CategoriesBean {

        private ListDataModel<Category> modelCategories = new ListDataModel<Category>();

public ListDataModel<Category> getModelCategories() {
    return modelCategories;
}

public void setModelCategories(ListDataModel<Category> modelCategories) {
    this.modelCategories = modelCategories;
}
 public CategoriesBean() {
            modelCategories.setWrappedData(DAO.getDAO().getCategories());
        }
    }

DAO.java

public class DAO {

    private static DAO instance = new DAO();
    private List<Category> categories = new ArrayList<Category>();

    {
      Category smartphones = new Category(1, "Smartphones");    
      Category consoles = new Category(2, "Consoles");
      categories.add(smartphones);
      categories.add(consoles);

    }

    public static DAO getDAO() {
        return instance;
    }

    public List<Category> getCategories() {
        return this.categories;
    }

这是网站来源:

<form id="j_idt2" name="j_idt2" method="post" action="/Shop/categories.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt2" value="j_idt2" />


        <ul>
        </ul><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAAJVSQWvUQBR+Tbp2G1Zpt9KL6KkIgmSp2IuLuIvt0sXULqSCxYPOZqebWSfJOPOym/RQ8B8InoSKVw/e/AXVgyAo6FFv3j16dyaN3R704EBe8vK+9+Z773tvfkJFKAmLIzImboqMu5tEhVtEVOa+Hb1ffvTFBqsDDk/IoEMCTGQX5jGUVIUJH2TiVgvMqU2q2i7ox0Y4M3rIBriaSlh64BV1OYmH7nZ/RANsPvt8/9WCusItgEzohIrQJ30CB2Ab70CCa3Iyd48EVLlBEokkpjG697q3/3yv9GQiqMT8Ds0VlKeuC0o4N71wI06j00GB4BBEyfopUqWbXpo23ZaS5B5TmD39evHFB/LShpkuzCq2TwuW9mTWWJ10+e/sfCRIN/VMqPTJmMrdj29vPj/8tGWB5cF8wIlSd0lEEerFSBqGYcPXZOJh0wNH6ZxBUQNh+RjBkoZPJSOc7ZM+p81MiLEZEyhja7qbS/p6V6VxScZYTlG57V7P626slzjNee0fQBYJ7q7TPZJy7Bz/XGkLwfOd5DGNf72+unvYGrVqZnaTC7DYCDS/YSKZrpGFGHGAme13338cZZmWbe3/ZOtJNtblTgtk+M4hnJ+KtBMSbEvqa7Im6GigVShhlc0VE3FKx9i6kflssYHXbhSv6yfLVTWrpsPVso/cOBUDWj3BWEJkvwHxOyj9FQMAAA==" autocomplete="off" />
</form>

【问题讨论】:

  • 你能显示Category类吗,因为这段代码对我有用。

标签: java eclipse jsf tomcat dao


【解决方案1】:

使用 ui:repeat 时无需调用 DataModel 列表:

        <ui:repeat var="category" value="#{categoriesBean.categories}">
            <li><h:outputText value="#{category.name}">
                </h:outputText>
                 </li>
        </ui:repeat>

然后,当您调用服务时,大部分时间都是数据库交互,您在 bean 中注入服务。为此,您应该使用 @PostConstruct 属性(在您的情况下不需要)。

    @ManagedBean
    @RequestScoped
    public class CategoriesBean {
        // this is not needed but that's how you'd get from db.
        //@Inject
        //private MyService service;

        private List<Category> categories;

       // getters and setters

       @PostConstruct // in your case this is not needed but it is if you use injection
       public void init() {
           categories = yourService.getCategories(); // this is not needed.
           //instead you can add it like this.
           categories = new ArrayList<Category>();
           categories.add(1, "blabla");
        }
   }

【讨论】:

  • @Valery 您是否检查过您的列表不为空及其大小?
  • @Valery 请使用信息编辑您的原始帖子。您在编辑帖子时难以阅读的答案中发布了信息。
  • @Valery 如果有帮助,请点赞并接受答案。
  • 尝试重建并重新启动服务器,因为它应该可以工作。
猜你喜欢
  • 2013-11-22
  • 2021-11-08
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 2020-01-31
  • 2013-10-06
相关资源
最近更新 更多