Primefaces TreeTable - 无法实例化类

时间:2012-08-16 15:29:23

标签: java jsf primefaces

我正在尝试从我正在解析的属性文件中创建treetable,并且我不断使用下面的代码获取错误“无法实例化类”。我不确定为什么它会抛出这个例外。

for(Map.Entry<Object, Object> prop : props.entrySet()) {
    String key = ((String)prop.getKey()).replace("_", " ");
    String value = (String)prop.getValue();

    String[] parts = key.split("\\.");

    if(parts.length > 4) {
        boolean topLevelExists = false;

        // if there are already child nodes
        if(root.getChildCount() > 0) {
            for(TreeNode n : root.getChildren()) {
                System.out.println("n = "+n);
                System.out.println("n.data = "+((Menu)n.getData()).getName());
                System.out.println("n.getType = "+n.getType());

                if(n.getType().equals(parts[2])) {  
                    System.out.println("they equal");
                    topLevelExists = true;
                    if(topLevelExists == true) {
                        System.out.println("topLevelExists");
                        TreeNode node = new DefaultTreeNode(parts[2], new Menu(parts[2], value), n);
                    }
                } else {
                    TreeNode node = new DefaultTreeNode(parts[2], new Menu(parts[2], value), root);  // FAILS ON THIS LINE WITH THE EXCEPTION BELOW
                }
            }
        // else we are creating the first node
        } else {
            TreeNode node = new DefaultTreeNode(parts[2], new Menu(parts[2], value), root);
        }


    }
}

Menu.java

public class Menu {

    private String name;
    private String path;

    public Menu(String name, String path) {
        this.name = name;
        this.path = path;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
}

抛出异常

com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: edu.mayo.ccs.alliance.menu.backing.MenuBacking.
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:112)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:193)
    at org.primefaces.component.api.UITree.getValue(UITree.java:99)
    at org.primefaces.component.treetable.TreeTableRenderer.encodeTbody(TreeTableRenderer.java:260)
    at org.primefaces.component.treetable.TreeTableRenderer.encodeRegularMarkup(TreeTableRenderer.java:208)
    at org.primefaces.component.treetable.TreeTableRenderer.encodeMarkup(TreeTableRenderer.java:132)
    at org.primefaces.component.treetable.TreeTableRenderer.encodeEnd(TreeTableRenderer.java:88)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:884)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1681)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:854)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1674)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1677)
    at org.primefaces.component.accordionpanel.AccordionPanelRenderer.encodeTab(AccordionPanelRenderer.java:218)
    at org.primefaces.component.accordionpanel.AccordionPanelRenderer.encodeTabs(AccordionPanelRenderer.java:147)
    at org.primefaces.component.accordionpanel.AccordionPanelRenderer.encodeMarkup(AccordionPanelRenderer.java:91)
    at org.primefaces.component.accordionpanel.AccordionPanelRenderer.encodeEnd(AccordionPanelRenderer.java:71)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:884)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1681)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1677)
    at org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:279)
    at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:237)
    at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:128)
    at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:69)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:884)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1681)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1677)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1677)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:509)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at edu.mayo.ccs.alliance.util.BrowserNotSupportedFilter.doFilter(BrowserNotSupportedFilter.java:151)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at edu.mayo.ccs.ctsu.sso.CustomAuthenticator.invoke(CustomAuthenticator.java:42)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java:343)
    at edu.mayo.ccs.alliance.menu.backing.MenuBacking.parseMenu(MenuBacking.java:151)
    at edu.mayo.ccs.alliance.menu.backing.MenuBacking.<init>(MenuBacking.java:96)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
    ... 64 more

1 个答案:

答案 0 :(得分:2)

到目前为止,当你还在迭代它时,你正在修改root

for (TreeNode n : root.getChildren()) {
    // ...
    TreeNode node = new DefaultTreeNode(parts[2], new Menu(parts[2], value), root);  // FAILS ON THIS LINE WITH THE EXCEPTION BELOW
    // ...
}

new DefaultTreeNode会将new Menu添加为给定root的子项。但是,这将导致迭代失败,因为后备集合已同时被修改。这就是ConcurrentModificationException试图告诉你的全部内容。

您需要相应地重写代码,以便在迭代new DefaultTreeMode时未创建root root子项。我基本上可以看到两种方法来解决这个问题:重新设计代码,这样你根本不需要遍历root,或者在某些ListMap中收集新的孩子然后将它们添加到root。具体的功能要求并不完全清楚,所以我不能建议哪一个是正确的解决方案。