从“选择一个菜单"中选择“定制对象" [英] Pick Custom Object from Select one menu JSF

查看:74
本文介绍了从“选择一个菜单"中选择“定制对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个菜单中选择一个自定义对象.它既不显示错误,也不显示值.我应该怎么办?

I want to pick a custom object from select one menu. It neither shows an error nor values. What should I do?

我的xhtml文档:

<h:panelGrid columns="2">
    <p:outputLabel value="" />
        <p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" >
            <f:selectItem itemLabel="-- Select Currency--" itemValue="#{null}"/>
        <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency.currencyId}" itemLabel="#{currency.currencyName}" > 
            </f:selectItems>
        <p:ajax update="currencyOut" />
        </p:selectOneMenu>
        <p:outputLabel value="Currency Id : #{CurrencyMB.currency.currencyId}" id="currencyOut" />
</h:panelGrid>

我的ManagedBean类:

My managedBean class:

@ManagedBean(name = "CurrencyMB")
@RequestScoped
public class CurrencyManagedBean {

private Currency currency;
private List<Currency> currencyList;


public Currency getCurrency() {
        return currency;
    }

public void setCurrency(Currency currency) {
        this.currency = currency;
    }
public List<Currency> getCurrencyList() {
        currencyList = new ArrayList<Currency>();
        currencyList.addAll(getiCurrencyService().getCurrencies());
        return currencyList;
    }

public void setCurrencyList(List<Currency> currencyList) {
        this.currencyList = currencyList;
    }

}

推荐答案

您正在尝试将类Currency的Java对象映射到作为HTTP请求参数的字符串.转换器适用于需要从其字符串表示形式创建对象的情况,反之亦然,例如在您遇到的情况下.

You are trying to map a Java object of class Currency to a string that comes as a HTTP request parameter. A converter is intended to be used in a situation when you need to create an object from a its string representation, and vice versa, like in the situation you faced.

基本上有两种方法.

1.使用转换器.

使用这种方法,您可以将项目值定义为Currency对象,并使用转换器从对象创建字符串表示形式,然后从字符串重新创建对象.对于转换器部分,只需遵循Luiggi所指向的教程.基本上,您需要创建一个实现Converter的类,并用@FacesConverter("currencyConverter")进行注释,以便能够通过id引用转换器,例如JSF标签的converter="currencyConverter"属性:

With this approach you define item value as a Currency object and use a converter to create string representation from an object and recreate an object back from a string. For the converter part, just follow the tutorial Luiggi pointed at. Basically you need to create a class that implements Converter, annotate it with @FacesConverter("currencyConverter") to be able to refer to the converter by id, like in converter="currencyConverter" attribute of a JSF tag:

<p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" converter="currencyConverter">
    <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency}" itemLabel="#{currency.currencyName}" /> 
    <p:ajax update="currencyOut" />
</p:selectOneMenu>

2.使用普通的String(或Java基本包装器).

2. Utilize plain Strings (or java primitive wrappers).

使用这种方法,您可以将项目值以及用户选择绑定到String类型的bean属性,而不是实际对象.以这种方式使用它,您将不需要任何转换器,并且将为您设置字符串值:

With this approach you bind item values, as well as user selection to a bean property of String type, and not to an actual object. Using it this way you won't need any converter, and string values will be set for you:

<p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currencyName}">
    <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency.currencyName}" itemLabel="#{currency.currencyName}" /> 
    <p:ajax update="currencyOut" />
</p:selectOneMenu>

最后,值得阅读答案中的问题为什么要选择OneMenu将ItemLabel发送到转换器?.

Finally, it is worth reading the question to the answer Why selectOneMenu Send ItemLabel to the converter?.

这篇关于从“选择一个菜单"中选择“定制对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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