1

如何为具有复合主键的实体编写转换器?

我想使用 URl 编辑实体,例如 `\edit_representative.xhtml?emp_id=12345&project_id=45

我正在使用 Omnifaces,所以我对单个主键有类似的东西

<o:viewParam name="emp_id" value="#{projectRepEdit.projectRep}"
converter="#{projectRepConverter}"
converterMessage="Bad request. Unknown Project Representative." required="true"
requiredMessage="Bad request. Please use a link from within the system." />

但我如何将员工正在处理的 project_id 传递给转换器

编辑

根据 BalusC 的回答,我使用了第二个选项,并在我的projectRepConverter转换器中有以下内容

String project_id = (String) component.getAttributes().get("project_id");
String emp_id= (String) component.getAttributes().get("emp_id");

ProjectRep prjRep = prjRepRepository.getByEmpIdAndProjectId(emp_id,project_id);
return prjRep;

在小脸上

<f:metadata>
<o:viewParam name="project_id" value="#{projectRepEdit.project}"
    converter="#{projectConverter}"
    converterMessage="Bad request. Unknown Project." required="true"
    requiredMessage="Bad request. Please use a link from within the system." />

<o:viewParam name="badge" value="#{projectRepEdit.projectRep}"
    converter="#{projectRepConverter}"
    converterMessage="Bad request. Unknown Project Rep." required="true"
    requiredMessage="Bad request. Please use a link from within the system.">
    <f:attribute name="project_id" value="#{param.project_id}" />
    <f:attribute name="emp_id" value="#{param.emp_id}" />
</o:viewParam>
4

1 回答 1

2

<f:viewParam>the和 the都不<o:viewParam>支持采用多个参数。您需要手动获取参数。我可以想到两种基本方法:

  1. 在转换器中将其作为请求参数获取:

    String project_id = FacesLocal.getRequestParameter(context, "project_id");
    

  2. 或将其作为组件属性传递:

    <o:viewParam ...>
        <f:attribute name="project_id" value="#{param.project_id}" />
    </o:viewParam>
    

    这样您就可以在转换器中按如下方式获取它:

    String project_id = (String) component.getAttributes().get("project_id");
    

选择哪种方式取决于转换器的预期可重用性。例如,您可以使用第二种方式将project_id属性名称重命名为 orsocompound_key并使其可重用。

于 2014-06-17T08:59:22.510 回答