2016-05-31 4 views
1

私はバッキング構成要素との複合コンポーネント製:複合コンポーネントの複合コンポーネントのバッキングコンポーネントでEntityManagerにアクセスする方法は?

て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:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:composite="http://java.sun.com/jsf/composite" xmlns:c="http://java.sun.com/jsp/jstl/core"> 

    <composite:interface componentType="editorCompositeComponent"> 
     <!-- ...bunch of attributes... --> 
     <composite:attribute name="content" type="java.lang.String" default="" /> 
    </composite:interface> 

    <composite:implementation> 
     <!-- ... other components ... --> 
     <p:editor widgetVar="editorWidget" value="#{cc.attrs.content}" width="600" maxlength="8000" /> 
     <p:commandButton action="#{cc.save(cc.attrs.caseId)}" value="Save" /> 
    </composite:implementation> 

</html> 

バッキングコンポーネント:

だから、
@FacesComponent("editorCompositeComponent") 
public class EditorCompositeComponent extends UINamingContainer { 

    private String content; 
    // bunch of other variables 

    public void save(String caseId) { 

    MemoFile memoFile = new MemoFile(); 
    memoFile.setContent(content); 
    memoFileService = new MemoFileService(); 
    // Normally this service would be Injected but Injection 
    // isn't possible in @FacesComponent 

    memoFileService.save(memoFile); 
    // the save-method just calls EntityManager's merge etc. 
    // It works well in all the ManagedBeans 
    } 
    // all the getters and setters 
} 

を、ものを注入しないとすることはできませんそれによってEntityManagerが見つかるので、どのようにコンポジットコンポーネントでエディタの内容を永続させるのでしょうか?

答えて

1

依存コンポーネント注入は、UIコンポーネントではサポートされていません。それは責任の密接なつながりのあまりにも多すぎます。 UIコンポーネントのインスタンスは、コンテナによって管理されていないと想定されます。

タスクのための別の要求スコープ管理対象Beanを作成することをお勧めします。

@Named 
@RequestScoped 
public class EditorCompositeBean { 
    // ... 
} 

あなたは、そのアクションメソッドに複合コンポーネントのインスタンスを渡すことができます。

<p:commandButton ... action="#{editorCompositeBean.save(cc)}" /> 

またはその代わりにモデルとしてそのBeanを使用します。

<composite:interface componentType="editorCompositeComponent"> 
    <composite:attribute name="value" type="com.example.EditorCompositeBean" /> 
</composite:interface> 

<composite:implementation> 
    <p:editor ... value="#{cc.attrs.value.content}" /> 
    <p:commandButton ... action="#{cc.attrs.value.save}" /> 
</composite:implementation> 
+0

ありがとうございました。私は実際にコンポーネントのバッキングBeanとしてManagedBeanを最初に持っていましたが、同じコンポジットコンポーネントの複数のインスタンスを同じビューに配置したいので、そのために@FacesComponentを作成する必要があります。 –

関連する問題