Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf F:选择显示类而非值的项_Jsf - Fatal编程技术网

Jsf F:选择显示类而非值的项

Jsf F:选择显示类而非值的项,jsf,Jsf,我是facelets的新手,我已经使用netbeans生成了一个项目,但是我很难使用标签 我有 <h:selectOneMenu id="country" value="#{organisation.organisation.country}" title="Country" > <f:selectItems value="#{country.countryItemsAvailableSelectOne}"/> &l

我是facelets的新手,我已经使用netbeans生成了一个项目,但是我很难使用标签

我有

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

在select中,我得到classpath.Country[iso=GB],我可以看到它是一个对象,但我确实希望看到Country.prinableName值。 我看了半天,画了一张空白 感谢您的帮助

发生了什么事,请调用所有给定对象的方法

您必须使用或使用一个简单的方法来管理它。一个非常简单的例子:

price.xhtml:

<h:selectOneMenu id="priceMenu" value="#{priceBean.selectedPrice}">
    <f:selectItems value="#{priceBean.prices}" />
</h:selectOneMenu>
..
private String selectedPrice;
..
public String getSelectedPrice() {
    return selectedPrice;
}

public void setSelectedPrice(String newPrice) {
    selectedPrice = newPrice;
}
..
public List<SelectItem> getPrices() {
    List<SelectItem> retVal = new ArrayList<SelectItem>();

    retVal.add(new SelectItem("2"));
    retVal.add(new SelectItem("4"));
    retVal.add(new SelectItem("6"));

    return retVal;
}

PriceBean.java:

<h:selectOneMenu id="priceMenu" value="#{priceBean.selectedPrice}">
    <f:selectItems value="#{priceBean.prices}" />
</h:selectOneMenu>
..
private String selectedPrice;
..
public String getSelectedPrice() {
    return selectedPrice;
}

public void setSelectedPrice(String newPrice) {
    selectedPrice = newPrice;
}
..
public List<SelectItem> getPrices() {
    List<SelectItem> retVal = new ArrayList<SelectItem>();

    retVal.add(new SelectItem("2"));
    retVal.add(new SelectItem("4"));
    retVal.add(new SelectItem("6"));

    return retVal;
}
。。
私有字符串selectedPrice;
..
公共字符串getSelectedPrice(){
返回所选价格;
}
public void setSelectedPrice(字符串newPrice){
selectedPrice=新价格;
}
..
公开价目表{
List retVal=new ArrayList();
retVal.add(新的SelectItem(“2”));
retVal.add(新选择项(“4”);
retVal.add(新选择项(“6”);
返回返回;
}

进一步的资料。如果您想直接使用特殊对象,例如名为
Price
的对象,则必须使用转换器

既然您谈论的是Facelets,我就假设JSF2.x

首先,HTML是一个完整的字符串。JSF生成HTML。默认情况下,非
String
Java对象通过
toString()
方法转换为它们的
String
表示,而JSF生成HTML。要在这些Java对象和
String
之间正确转换,您需要一个

我假设您的
Country
对象已经有了
equals()
方法,否则验证稍后将失败并显示“validation error:Value not valid”,因为所选对象在测试任何可用项的
equals()
时都不会返回
true

我还将对命名进行一些更改,因为
{country}
是一个令人困惑的托管bean名称,因为它显然不代表
country
类的实例。我将其称为
Data
,它应该保存应用程序范围的数据

@ManagedBean
@应用范围
公共类数据{
私有静态最终列表国家=PopulateItemSomeHow();
国家/地区公共列表(){
返回国;
}
// ...
}
我假设
Country
类有两个属性
code
name
。我假设接收所选国家/地区的托管bean具有
私有国家/地区
属性。在
中,需要循环使用
{data.countries}
并将国家对象指定为项目值,将国家名称指定为项目标签


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

@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将在表单提交时将提交的国家代码转换回fullworthy
country
对象

在JSF1.x中,原理没有太大不同。在本博客中,您可以找到两个基本的启动示例:.

如果您在

<h:selectOneMenu value="#{bean.country}">

谢谢你所有的假设都是正确的,一个很好的解释将通过这个例子起作用,它也将在其他方面帮助我