将多个视图参数传递给具有复合主键的实体的转换器

时间:2014-06-16 21:39:43

标签: jsf converter omnifaces multiple-value viewparams

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

我想用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;

并在facelet中

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

1 个答案:

答案 0 :(得分:2)

<f:viewParam><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");
    

  3. 选择哪种方式取决于转换器的可重用性。例如,您可以使用第二种方式将project_id属性名称重命名为compound_key左右,并使其可重复使用。