使用p:selectOneMenu从一个对象列表中选择UUID,然后输入另一个对象(作为外键)

时间:2019-07-12 16:35:01

标签: jsf selectonemenu converters

我有两个实体/类(客户,订单),分别带有相应的模型bean,jsf页面和支持bean(=控制器bean)。这两个实体的ID都是从类型UUID生成的。一切运行良好。

现在,在“订单”页面中,我试图引入一个<p:selectOneMenu>来选择一个客户(在后台为一个UUID),并将该客户的UUID(->来源)输入到订单中(输入“订单”)。 uuid_Customer'(类型为UUID)(-> target)作为外键。简而言之:源实体和目标实体是不同的。

显示了DropDown,并且我可以选择正确的客户列表。在所有情况下,phaseListener都会显示所选客户的正确UUID。因此,我认为<f:selectItems>标签没有问题。

问题::提交页面时出现验证错误“验证错误:值无效”。 (目标)字段“ order.uuid_Customer”(类型UUID)在jsf生命周期结束之前一直为空。

我看到了两个可能的错误来源:

1)我误解了两个“值”属性:

<p:selectOneMenu value="#{selectedOrder.uuid_Customer}" ...(=目标)

<f:selectItems value="#{order_Edit_C.customers}" ...(=源)

2)我误解了转换器的功能。    我不确定getAsObject方法应该返回什么。我在UUID中返回类型为UUID的对象order.uuid_Customer。我应该返回对象Customer吗?如果为“是”,则:我应该在哪里提取UUID并将其提交给order.uuid_Customer

order_Edit.xhtml(摘录):

<p:inputText id="a_uuid"
                value="#{selectedOrder.uuid}"
                readonly="true">
</p:inputText>
<p:inputText id="a_name"
                value="#{selectedOrder.name}">
</p:inputText>

...

<p:selectOneMenu id="sOM_Order_Edit"
                    value="#{selectedOrder.uuid_Customer}"
                    converter="customer_Converter">
    <f:selectItem itemLabel="Select One Item"/>
    <f:selectItems value="#{order_Edit_C.customers}"
                    var="customer" itemValue="#{customer}"
                    itemLabel="#{customer.external_ID} - #{customer.name}" />
    <f:validator validatorId="myValidator" />
</p:selectOneMenu>

这些是phaseListener行选择一个客户(显示的UUID是该客户的正确客户)。

16:22:32,956 INFO  [stdout] (default task-1) ### 1 ### START PHASE RESTORE_VIEW 1         #####
16:22:32,971 INFO  [stdout] (default task-1) ### 1 ### END PHASE   RESTORE_VIEW 1         #####
16:22:32,971 INFO  [stdout] (default task-1) ### 2 ### START PHASE APPLY_REQUEST_VALUES 2 #####
16:22:32,971 INFO  [stdout] (default task-1) - getAsObject: Customer 1 / c582928e-034f-40d5-90ad-ff0901753ca6
16:22:32,971 INFO  [stdout] (default task-1) ### 2 ### END PHASE   APPLY_REQUEST_VALUES 2 #####
16:22:32,971 INFO  [stdout] (default task-1) ### 3 ### START PHASE PROCESS_VALIDATIONS 3  #####
16:22:32,971 INFO  [stdout] (default task-1) - getAsObject: Customer 1 / c582928e-034f-40d5-90ad-ff0901753ca6
16:22:32,971 INFO  [stdout] (default task-1)   MyValidator validate: c582928e-034f-40d5-90ad-ff0901753ca6
16:22:32,987 INFO  [stdout] (default task-1) ### 3 ### END PHASE   PROCESS_VALIDATIONS 3  #####
16:22:32,987 INFO  [stdout] (default task-1) ### 6 ### START PHASE RENDER_RESPONSE 6      #####
16:22:33,003 INFO  [stdout] (default task-1) ### 6 ### END PHASE   RENDER_RESPONSE 6      #####

这些是提交页面的phaseListener行(请参见空的uuid_Customer:null):

17:44:41,294 INFO  [stdout] (default task-1) ### 1 ### START PHASE RESTORE_VIEW 1         #####
17:44:41,310 INFO  [stdout] (default task-1) ### 1 ### END PHASE   RESTORE_VIEW 1         #####
17:44:41,310 INFO  [stdout] (default task-1) ### 2 ### START PHASE APPLY_REQUEST_VALUES 2 #####
17:44:41,310 INFO  [stdout] (default task-1) ### 2 ### END PHASE   APPLY_REQUEST_VALUES 2 #####
17:44:41,310 INFO  [stdout] (default task-1) ### 3 ### START PHASE PROCESS_VALIDATIONS 3  #####
17:44:41,310 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,310 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,310 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,310 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,310 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,326 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,326 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,326 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,326 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,326 INFO  [stdout] (default task-1) - getAsObject: Customer 1 / c582928e-034f-40d5-90ad-ff0901753ca6
17:44:41,326 INFO  [stdout] (default task-1)   MyValidator validate: c582928e-034f-40d5-90ad-ff0901753ca6
17:44:41,341 INFO  [stdout] (default task-1) ### 3 ### END PHASE   PROCESS_VALIDATIONS 3  #####
17:44:41,341 INFO  [stdout] (default task-1) ### 6 ### START PHASE RENDER_RESPONSE 6      #####
17:44:41,341 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,357 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,357 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,357 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,357 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,357 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,357 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,357 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,357 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,372 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,372 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,372 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,372 INFO  [stdout] (default task-1)   - Order_P getSelectedOrder: je.de.model.entities.Order.Order@fc402e9
17:44:41,372 INFO  [stdout] (default task-1)   - uuid: ddbbb88f-2d77-4332-8121-f14d9b873d7f
17:44:41,372 INFO  [stdout] (default task-1)   - uuid_Contact: null
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: NULL / NULL
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 3 / 8d3908bf-700f-470f-ba15-42ebeba662b4
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 2 / 96be56c7-eb04-4e19-b43e-fb6d83373bfd
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 5 / b16a9f77-0644-44d4-b20d-038a0971e422
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 6 / be876edc-af63-435a-9d1b-5f7742400979
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 1 / c582928e-034f-40d5-90ad-ff0901753ca6
17:44:41,372 INFO  [stdout] (default task-1) - getAsString: Customer 4 / f41b195a-bfc9-4c6b-872c-ad21b37dab89
17:44:41,388 INFO  [stdout] (default task-1) ### 6 ### END PHASE   RENDER_RESPONSE 6      #####

和转换器(两种方法均按预期返回值):

@FacesConverter(value = "customer_Converter")
public class Customer_Converter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String uuid) {
        ValueExpression valueExpression =
                facesContext.getApplication().getExpressionFactory()
                        .createValueExpression(facesContext.getELContext(),
                                "#{order_Edit_C}", Order_Edit_C.class);
        Order_Edit_C customers = (Order_Edit_C)valueExpression.getValue(facesContext.getELContext());
        Customer customer = customers.getCustomer(uuid);
        System.out.println("- getAsObject: " + customer.getExternal_ID() + " / " + customer.getUuid());
        return customer.getUuid();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object customer) {
        //System.out.println("customer.getClass(): " + customer.getClass());
        Customer x = null;
        if (customer instanceof Customer) {
            Object o = (Object) customer;
            Customer c = (Customer) o;
            x = c;
        }
        if (x != null) {
            System.out.println("- getAsString: " + x.getExternal_ID() + " / " + x.getUuid());
            return x.getUuid().toString();
        }
        else {
            System.out.println("- getAsString: " + "NULL"             + " / " + "NULL");
            return null;
        }
    }

}

依赖项:

    <dependencies>
<!--
JAVA
-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
<!--
HIBERNATE
-->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.3.1.Final</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>dom4j</groupId>
                    <artifactId>dom4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.10.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--
PRIMEFACES
-->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.2</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>primefaces-extensions</artifactId>
            <version>6.2.4</version>
        </dependency>
<!--
MYSQL
-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

对于所有以适当方式填写“ order.uuid_Customer”字段(类型UUID)的提示,我表示感谢。

0 个答案:

没有答案