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

查看:22
本文介绍了如何从列表创建 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(s) 已被已入队,但可能尚未显示.sourceId=iterateCategories:0:iterateItems:2:lightForm:myRadio[severity=(ERROR2)、summary=(Conversion Error设置值'huhu.model.generated.Item@3ae5e1dc' 用于'空转换器'.),detail=(转换错误设置值'huhu.model.generated.Item@3ae5e1dc' for 'null Converter'.)]

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 输出中.如果您的对象没有重写此方法,那么它将是 javadoc:

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 方法返回一个字符串,该字符串由该对象作为实例的类的名称、at 符号字符 @ 和对象哈希码的无符号十六进制表示.换句话说,这个方法返回一个字符串等于:

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 的具体实例.但是,由于显然没有为自定义类型注册转换器(错误消息已经暗示了这一点:null converter"),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 JSF 实用程序库 OmniFaces 以便转换器将改为使用 <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天全站免登陆