从backing bean添加复合组件

时间:2017-02-14 15:34:30

标签: java jsf primefaces

我在仪表板上工作,我想从bean动态添加小部件到我的jsf页面,我试过这段代码

public UIComponent  addWidget(UIComponent parent,String libraryName, String ressourceName, String id){
            FacesContext context = FacesContext.getCurrentInstance();
            Application application = context.getApplication();
            FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);

            // This basically creates <ui:component> based on <composite:interface>.
            Resource resource = application.getResourceHandler().createResource(ressourceName, libraryName);

            UIComponent composite = application.createComponent(context, libraryName, ressourceName);
            composite.setId(id); // Mandatory for the case composite is part of UIForm! Otherwise JSF can't find inputs.

            // This basically creates <composite:implementation>.
            UIComponent implementation = application.createComponent(UIPanel.COMPONENT_TYPE);
            implementation.setRendererType("javax.faces.Group");
            composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, implementation);

            // Now include the composite component file in the given parent.

            parent.getChildren().add(composite);
            parent.pushComponentToEL(context, composite); // This makes #{cc} available.

            try {
                faceletContext.includeFacelet(implementation,resource.getURL());
                RequestContext.getCurrentInstance().update("uf:name");
            } catch (IOException e) {
               e.printStackTrace(System.out);
            } finally {
                parent.popComponentFromEL(context);

            }

        return composite;
    }

但它没有添加任何内容,我的小部件被定义为复合组件.ex:

0 个答案:

没有答案