Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSF-无法显示ManagedBean中的对象列表_Java_Eclipse_Jsf_Tomcat_Dao - Fatal编程技术网

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

Java JSF-无法显示ManagedBean中的对象列表,java,eclipse,jsf,tomcat,dao,Java,Eclipse,Jsf,Tomcat,Dao,也许有人知道我为什么不能在我的应用程序中显示类别列表。filecategories.xhtml仅显示单词类别。类类别在构造函数中设置id和名称 categories.xhtml <ui:component> <h:form> <h4>Categories</h4> <ul> <ui:repeat var="category" value="#{categoriesBe

也许有人知道我为什么不能在我的应用程序中显示类别列表。filecategories.xhtml仅显示单词类别。类类别在构造函数中设置id和名称

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

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

你能显示Category类吗,因为这段代码对我有用。@Valery你检查过你的列表是否为非空及其大小吗?@Valery,请用这些信息编辑你的原始帖子。您在“答案”中发布的信息在编辑帖子时很难阅读。@Valery请投票并接受答案(如果有帮助)。请尝试重建并重新启动服务器,因为它应该可以工作。