F:selectItems显示类不是值 [英] F:selectItems showing the class not a value

查看:67
本文介绍了F:selectItems显示类不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Faceface的新手,我已经使用netbeans生成了一个项目,但是我在标签上苦苦挣扎.

I'm new to facelets and I have generated a project using netbeans but I struggling with the tag.

我有

<h:selectOneMenu id="country" value="#{organisation.organisation.country}" title="Country" >
                <f:selectItems value="#{country.countryItemsAvailableSelectOne}"/>
            </h:selectOneMenu>

在选择中,我得到的classpath.Country [iso = GB]是一个对象,但我确实想查看country.prinableName值. 我已经看了半天,画了一个空白 感谢您的帮助

In the select I get classpath.Country[iso=GB] which I can see is an object but I really want to see the the country.prinableName value. I've looked at this for half a day and have drawn a blank Thanks for any help

推荐答案

由于您在谈论Facelets,因此我假设使用JSF 2.x.

Since you're talking about Facelets, I'll assume JSF 2.x.

首先,HTML是唯一的String. JSF生成HTML.默认情况下,当JSF生成HTML时,非String Java对象通过toString()方法转换为它们的String表示形式.要在这些Java对象和String之间正确转换,您需要一个

To start, HTML is one and all String. JSF generates HTML. By default, non-String Java objects are by toString() method converted to their String representation while JSF generates the HTML. To properly convert between those Java objects and String, you need a Converter.

我假设您的Country对象已经具有equals()方法equals()的任何可用项目时不会返回true.

I assume that your Country object has already the equals() method properly implemented, otherwise validation will later fail with "Validation error: Value not valid" because the selected object doesn't return true on testing the equals() for any of the available items.

由于#{country}是一个令人困惑的托管bean名称,因此我还将对命名进行一些更改,因为它显然不表示Country类的实例.我将其命名为Data,其中应包含应用程序范围的数据.

I'll also make a little change in the naming since #{country} is a confusing managed bean name because it does apparently not represent an instance of the Country class. I'll call it Data which should hold application wide data.

@ManagedBean
@ApplicaitionScoped
public class Data {

    private static final List<Country> COUNTRIES = populateItSomehow();

    public List<Country> getCountries() {
        return COUNTRIES;
    }

    // ...
}

我假设Country类具有两个属性codename.我假设接收选定国家/地区的托管Bean具有private Country country属性.在<f:selectItems>中,您需要遍历#{data.countries}并将国家对象指定为项目值,并将国家名称指定为项目标签.

I'll assume that the Country class has two properties code and name. I'll assume that the managed bean which receives the selected country has a private Country country property. In your <f:selectItems>, you need to loop over #{data.countries} and specify the country object as item value and the country name as item label.

<h:selectOneMenu value="#{bean.country}">
    <f:selectItems value="#{data.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>

现在,您需要为Country类创建一个Converter.我们将根据每个国家/地区唯一的国家/地区代码进行转换(对吗?).在getAsString()中,您将实现将Java对象转换为其将在HTML中使用的唯一String表示形式的代码.在getAsObject()中,您将实现将唯一的HTML字符串表示形式转换回Java对象的代码.

Now, you need to create a Converter for the Country class. We'll convert based on the country code which is unique for every country (right?). In the getAsString() you implement code which converts the Java object to its unique String representation which is to be used in HTML. In getAsObject() you implement code which converts the unique HTML String representation back to the Java object.

@FacesConverter(forClass=Country.class)
public class CountryConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return (value instanceof Country) ? ((Country) value).getCode() : null;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }

        Data data = context.getApplication().evaluateExpressionGet(context, "#{data}", Data.class);

        for (Country country : data.getCountries()) {
            if (country.getCode().equals(value)) {
                return country;
            }
        }

        throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country", value)));
    }

}

@FacesConverter将在JSF中自动注册它,并且JSF遇到Country类型的值表达式时将自动使用它.最终,您最终将国家/地区代码作为项目值并将国家/地区名称作为项目标签.提交表单后,JSF会将提交的国家/地区代码转换回完全有价值的Country对象.

The @FacesConverter will register it automatically in JSF and JSF will automatically use it whenever it encounters a value expression of the Country type. Ultimately, you end up with country code as item value and country name as item label. JSF will convert the submitted country code back to a fullworthy Country object upon form submission.

在JSF 1.x中,原理没有太大不同.在此博客中,您可以找到两个基本的启动示例: h:selectOneMenu中的对象.

In JSF 1.x the principle is not much different. In this blog you can find two basic kickoff examples: Objects in h:selectOneMenu.

这篇关于F:selectItems显示类不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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