Primefaces selectOneMenu中的CustomContent而不保留对象 [英] CustomContent in Primefaces selectOneMenu without persisting object

查看:54
本文介绍了Primefaces selectOneMenu中的CustomContent而不保留对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Primefaces SelectOneMenu( http://www.primefaces.org /showcase-labs/ui/selectOneMenu.jsf ),如演示中所述,带有自定义内容"版本.

问题在于,只有绑定到下拉字段的字段的实际值是用作value的对象的类型时,这些列才起作用.

以下工作原理(如果Bean.fieldSelectObject类型):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

以下内容不起作用(类型为StringBean.field):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt.value}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

在第一个版本中,xyz显示为第二列,而在第二个版本中则不显示.我的问题是,尽管我也可以只将value字段的字符串保存在那里,现在我需要在数据库中保存一个SelectObject类型的对象.

有解决这个问题的好方法吗?

解决方案

使用转换器将所选菜单项的值(bt.value)转换为与p:selectOneMenu(bean: .场地).

<p:selectOneMenu value="#{bean.field}" var="x" converter="yourconverterid">

I am using the Primefaces SelectOneMenu (http://www.primefaces.org/showcase-labs/ui/selectOneMenu.jsf) with the "Custom content" version as described in the demo.

The problem is that the columns only work if the actual value of the field, which is bound to the dropdown field, is of the type of the Object used as the value.

The following works (if Bean.field is of type SelectObject):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

The following does NOT work (Bean.field of type String):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt.value}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

In the first version, the xyz is displayed as second column, in the second version it's not. The problem I have is that now I need to save an object of type SelectObject in the database although I could as well just save the String of the value field there.

Is there a nice way to handle this?

解决方案

Use a Converter to convert the value of the selected menu item (bt.value) to that expected by the backing bean method tied to the p:selectOneMenu (bean.field).

<p:selectOneMenu value="#{bean.field}" var="x" converter="yourconverterid">

这篇关于Primefaces selectOneMenu中的CustomContent而不保留对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆