JSF - 无法显示ManagedBean中的对象列表

时间:2015-09-12 18:03:08

标签: java eclipse jsf tomcat dao

有人可能知道为什么我无法在我的应用中显示类别列表。文件类别.xhtml仅显示单词类别。类类别在构造函数中设置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>

1 个答案:

答案 0 :(得分:0)

使用ui时,您不需要调用DataModel列表:repeat:

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