简体   繁体   English

如何在复合组件的支持组件中访问 EntityManager?

[英]How to access EntityManager in the Backing Component of a composite component?

I made a composite component with a backing component:我制作了一个带有支持组件的复合组件:

Xhtml of the composite component:复合组件的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>

Backing component:支撑组件:

@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
}

So, can't inject stuff and can't thereby find EntityManager, so how would one go about persisting the contents of the editor in composite component?所以,不能注入东西,也不能因此找到 EntityManager,那么如何将编辑器的内容持久化到复合组件中呢?

Dependency injection isn't supported in UI components. UI 组件不支持依赖注入。 That's a bit too much of tight coupling of responsibilities.这有点过于紧密的职责耦合了。 UI component instances aren't supposed to be managed by the container. UI 组件实例不应由容器管理。

Your best bet is to create a separate request scoped managed bean for the task.最好的办法是为任务创建一个单独的请求范围托管 bean。

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

You could pass the composite component instance to its action method:您可以将复合组件实例传递给其操作方法:

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

Or use that bean as the model instead:或者使用该 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM