如何从列表创建Primefaces单选按钮? [英] How to create Primefaces radioButtons from List?

查看:94
本文介绍了如何从列表创建Primefaces单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从对象列表#{item.items3}创建一组单选按钮,并将选定的对象存储到#{cartBean.selectedChoice}中.现在,我并没有真正理解<f:selectItems><ui:repeat>所需的值之间的区别.我的代码看起来如何.到目前为止有明显的错误吗?

I want to create a set of Radiobuttons from a List of Objects #{item.items3} and store the selected object into #{cartBean.selectedChoice}. Now I don't really get the difference between the values needed for <f:selectItems> and <ui:repeat>. How does my code look. Any obvious mistakes so far?

<p:selectOneRadio id="myRadio" value="#{cartBean.selectedChoice}" layout="custom">
    <f:selectItems value="#{item.items3}"/>
</p:selectOneRadio>

<h:panelGrid columns="1">
    <ui:repeat var="choice" value="#{item.items3}" varStatus="choiceIndex">
        <p:radioButton id="choiceRadio" for=":iterateCategories:iterateItems:lightForm:myRadio" itemIndex="#{choiceIndex.index}" />#{choice.name}
    </ui:repeat>
</h:panelGrid>

此刻我遇到以下错误:

20:58:52,397信息 [javax.enterprise.resource.webcontainer.jsf.renderkit] (http-localhost-127.0.0.1-8080-1)警告:FacesMessage已被使用 已入队,但可能未显示. sourceId = iterateCategories:0:iterateItems:2:lightForm:myRadio [severity =(ERROR 2),摘要=(转换错误"设置值 'huhu.model.genic.Item@3ae5e1dc'(用于空转换器").), detail =(转换错误设置值 'huhu.model.genic.Item@3ae5e1dc'(表示空转换器".)]

20:58:52,397 INFO [javax.enterprise.resource.webcontainer.jsf.renderkit] (http-localhost-127.0.0.1-8080-1) WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=iterateCategories:0:iterateItems:2:lightForm:myRadio[severity=(ERROR 2), summary=(Conversion Error setting value 'huhu.model.generated.Item@3ae5e1dc' for 'null Converter'.), detail=(Conversion Error setting value 'huhu.model.generated.Item@3ae5e1dc' for 'null Converter'.)]

我不明白,哪里可能存在转换问题,因为只处理同一类的对象.

I don't understand, where there could be a conversion problem as only objects of the same class are dealt with.

推荐答案

JSF生成HTML. HTML基本上是一个大字符串.因此,非字符串类型的Java对象需要转换为字符串.如果遇到找不到内置转换器(NumberBooleanEnum)或自定义转换器(实现Converter的类)的类型,则将使用对象的默认toString()实现.将复杂的Java对象打印到HTML输出中.如果您的对象没有覆盖此方法,则它将是Object#toString()的默认实现,在

JSF generates HTML. HTML is basically one large string. Non-string typed Java objects needs therefore to be converted to string. If a type is encountered for which either no builtin converter (Number, Boolean and Enum) nor a custom converter (a class implementing Converter) is found, then the object's default toString() implementation will be used to print the complex Java object into the HTML output. If your object doesn't have this method overridden, then it will be the Object#toString()'s default implementation which is described in the javadoc:

Object的toString方法返回一个字符串,该字符串由对象是其实例的类的名称,符号字符@和该对象的哈希码的无符号十六进制表示组成.换句话说,此方法返回的字符串等于:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

在您的特定情况下,生成的HTML单选按钮元素将变为以下内容:

In your particular case, the generated HTML radio button element becomes the following:

<input type="radio" ... value="huhu.model.generated.Item@3ae5e1dc" />

(右键单击浏览器中的页面,然后选择查看源代码"以自己查看)

现在,提交此表单时,必须将由request.getParameter()收集的非常输入值huhu.model.generated.Item@3ae5e1dc(返回String!)转换回自定义类型Item的具体实例.但是,由于显然没有为自定义类型注册任何转换器(错误消息已经暗示了这一点:空转换器"),因此JSF无法将其转换回Item并将抛出此转换器异常.

Now, when this form is submitted, the very input value huhu.model.generated.Item@3ae5e1dc which is collected by request.getParameter() (which returns String!) has to be converted back to a concrete instance of your custom type Item. However, as there's apparently no converter been registered for the custom type (error message already hints this: "null converter"), JSF is unable to convert it back to Item and will throw this converter exception.

您应该真正提供一个自定义转换器,该转换器可以在Item及其唯一的String表示形式之间正确转换.通常,技术ID(例如数据库自动生成的PK)被用作唯一的String表示形式.转换器将如下所示:

You should really be providing a custom converter which properly converts between Item and its unique String representation. More than often a technical ID (such as the autogenerated PK from the database) is been used as unique String representation. The converter would then look like as follows:

@FacesConverter(forClass=Item.class)
public class ItemConverter implements Converter {

    @Override
    public void getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException {
        // Write code to convert Item to its unique String representation. E.g.
        return String.valueOf(((Item) modelValue).getId());
    }

    @Override 
    public void getAsObject(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
        // Write code to convert unique String representation of Item to concrete Item. E.g.
        return someItemService.find(Long.valueOf(submittedValue));
    }
    
}

或者,您可以使用 SelectItemsConverter OmniFaces 中的a>,以便转换器将<f:selectItem(s)>用作转换基础.这样,您无需为要在<f:selectItem(s)>中使用的每种自定义Java类型创建自定义转换器.另请参见 SelectItemsConverter展示页面:

Alternatively, you could use the SelectItemsConverter of the JSF utility library OmniFaces so that the converter will instead use the <f:selectItem(s)> as conversion basis. This way you don't need to create a custom converter for every custom Java type which you'd like to use in <f:selectItem(s)>. See also the SelectItemsConverter showcase page:

<p:selectOneRadio ... converter="omnifaces.SelectItemsConverter">

这篇关于如何从列表创建Primefaces单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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