OmniFaces の小さな適応を使用して、複合コンポーネントを別の UIComponent に動的に追加しようとしていますFaces.includeCompositeComponent(...)
。この質問の動作を再現しようとしましたが、うまくいきません。
適応されたメソッドのコードは次のとおりです ( OmniFaces v1.6から取得しました) 。
/**
* 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><my:resourceName></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;
}
そして、これは私がそれを使用しようとしている方法です
JSFUtils.includeCompositeComponent(panelGroup, "comp",
"../inc-templates/test.xhtml", JSFUtils.createUniqueId());
また、「test.xhtml」ファイルを作成し、WEB-INF/inc-templates/test.xhtml の下に配置しました。その内容は次のとおりです。
test.xhtml
<?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 タグを taglib.xml に追加しました。
<tag>
<tag-name>test</tag-name>
<source>../inc-templates/test.xhtml</source>
</tag>
問題は、コードを実行してコンポーネントを挿入しようとすると、次の例外が発生することです
Caused by: java.lang.NullPointerException: Argumentfehler: Parameter componentResource ist null
at com.sun.faces.util.Util.notNull(Util.java:314)
at com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:928)
at org.my.JSFUtils.includeCompositeComponent(JSFUtils.java:496)
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だけに変更しようとしましたが、何も機能しません... いつも同じエラーが発生します。
ところで、Websphere 8 にデプロイしようとしていて、ICEFaces 3 を使用しています。
前もって感謝します!