2

OmniFaces Faces.includeCompositeComponent(...)의 작은 변형을 사용하여 다른 UIComponent에 복합 구성 요소를 동적으로 추가하려고합니다. 나는 this question의 동작을 재현하려고 시도했지만 작동하지 않습니다."Faces.includeCompositeComponent"?를 사용하여 동적으로 구성 요소를 추가하는 방법 Websphere 8 및 ICEFaces 3

적응 방법에 대한 코드입니다

/** 
    * Create and include the composite component of the given library ane 
    * resource name as child of the given UI component parent and return the 
    * created composite component. This has the same effect as using 
    * <code>&lt;my:resourceName&gt;</code>. The given component ID must be unique 
    * relative to the current naming container parent and is mandatory for 
    * functioning of input components inside the composite, if any. 
    * 
    * @param parent The parent component to include the composite component in. 
    * @param libraryName The library name of the composite component. 
    * @param resourceName The resource name of the composite component. 
    * @param id The component ID of the composite component. 
    * @return The created composite component, which can if necessary be further 
    *   used to set custom attributes or value expressions on it. 
    * @since 1.5 
    */ 
    public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id) 
    { 
    // Prepare. 
    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(resourceName, libraryName); 
    UIComponent composite = application.createComponent(context, resource); 
    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()); 
    } 
    catch (IOException e) 
    { 
     throw new FacesException(e); 
    } 
    finally 
    { 
     parent.popComponentFromEL(context); 
    } 

    return composite; 
} 

(내가 OmniFaces v1.6에에서했다) 그리고이 미안 그것에게

JSFUtils.includeCompositeComponent(panelGroup, "comp", 
    "../inc-templates/test.xhtml", JSFUtils.createUniqueId()); 
를 사용하려고 시도하는 방법입니다 다음

또한 "test.xhtml"파일을 만들고 WEB-INF/inc-templates/test.xhtml 아래에 두었습니다. 다음은 그 내용이다 :

<tag> 
    <tag-name>test</tag-name> 
    <source>../inc-templates/test.xhtml</source> 
</tag> 
: 적어도

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:cc="http://java.sun.com/jsf/composite" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:composite="http://java.sun.com/jsf/composite"> 
    <!-- INTERFACE --> 
    <cc:interface> 

    </cc:interface> 

    <!-- IMPLEMENTATION --> 
    <cc:implementation> 
     <h:outputText value="TEST"/> 
    </cc:implementation> 
</html> 

마지막으로

test.xhtml, I've이처럼 taglib.xml에 test.xhtml 태그를 추가

코드를 실행하고 구성 요소를 삽입하려고하면 다음 예외가 발생합니다.

,393,210

및 org.my.JSFUtils.includeCompositeComponent (JSFUtils.java:496)에서 광고 사실 자원이 이전 라인에서 생성되지 않는 것을 알 UIComponent composite = application.createComponent(context, resource); 코드의 라인에 정확히 가리킨다 : Resource resource = application.getResourceHandler().createResource(resourceName, libraryName);

왜 이런 일이 일어날 수 있는지 전혀 알지 못합니까? 나는 심지어 inc-templates/test.xhtml 그리고 심지어는 test.xhtml 메서드를 호출 할 때 리소스의 위치를 ​​변경해 보았습니다.하지만 아무 것도 작동하지 않습니다 ... 나는 항상 같은 오류가 발생합니다. 그런데

은, 미안은 ICEFaces 3.

사전에

감사를 사용하여 WebSphere 8 미안에 배포하려고!

답변

1

복합 구성 요소를 태그 파일과 혼합하는 것 같습니다. 이 답변과 그 내부의 링크의 차이점을보고 이해하려면 When to use <ui:include>, tag files, composite components and/or custom components?

XHTML 파일은 복합 구성 요소를 명확하게 나타냅니다. 그러나 태그 파일처럼 취급하고 있습니다. 예를 들어 /resources 폴더 대신 태그 파일처럼 /WEB-INF 폴더에 배치되었습니다. 진정한 합성물처럼 구성없는 태그 파일 대신에 .taglib.xml 파일에 등록되었습니다.

정확하게 원하는 것이 무엇인지 명확하지 않습니다. 포함 파일, 태그 파일 또는 복합 구성 요소를 프로그래밍 방식으로 포함 하시겠습니까? 실제로 프로그래밍 방식으로 복합 구성 요소를 포함한다고 가정 해 봅시다 (그렇지 않으면 includeCompositeComponent()의 전체 사용은 완전히 무의미합니다).이 경우, (당신이 임의의 XHTML 템플릿 클라이언트에서 <my:test>를 사용할 때 그것은 또한 정말 작동 등의 방법으로) 모든 복합 구성 요소 튜토리얼에서와 같이 같은 /resources 폴더에 XHTML 파일을 이동 :

WebContent 
|-- WEB-INF 
| `-- lib 
|-- resources 
| `-- mycomponents 
|   `-- test.xhtml <--- 
`-- some.xhtml 

가 제거하십시오 불필요한 항목은 .taglib.xml이고 마지막으로 라이브러리 이름은 null이고 자원 이름은 /mycomponents/test.xhtml입니다.

includeCompositeComponent(parent, null, "/mycomponents/test.xhtml", uniqueId); 
+0

정말 고마워요! 생각한 것처럼, 프로그래밍 방식으로 복합 구성 요소를 포함하려고했습니다. 나는 네가 제안한 것을 했어. – jcrusoe