selectOneMenu转换器 [英] selectOneMenu Converter

查看:186
本文介绍了selectOneMenu转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在研究Primfaces页面,我必须让selectOneMenu从dataBase中获取项目,所以我试着这样做但我仍然对转换器有问题

Hi every body I'm working on Primfaces pages and I have to make a selectOneMenu wich get items from dataBase so I tried to make it this way but I still have problem's with the converter


所以我的源代码如下:

so my source Codes are the folowing :

selectOneMenu:

selectOneMenu :

<p:selectOneMenu id="devises" required="true" value="#{pret.devise}" effect="fade" converter="devise">  
    <f:selectItem itemLabel="Select One" itemValue="" />  
    <f:selectItems value="#{devise.listDevise()}" var="devise" itemLabel="#{devise.nomDevise}" itemValue="#{devise}"/>  
</p:selectOneMenu>

转换器代码:

@FacesConverter(value = "devise")
public class DeviseConverter implements Converter{  

    public static List<Devise> devises = Devise.listDevise();  

    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {  
        if (submittedValue.trim().equals("")) {  
            return null;  
        } else {  
            try {  
                int idDevise = Integer.parseInt(submittedValue);
                for (Devise p : devises) {  
                    if (p.getIdDevise()== idDevise) {  
                        return p;  
                    }  
                }
            } catch(NumberFormatException exception) {  
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Devise"));  
            }  
        }
        return null;  
    }  

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {  
        if (value == null || value.equals("")) {  
            return "";  
        } else {  
            return String.valueOf(((Devise) value).getIdDevise());  
        }  
    }
}

错误代码是:设计:验证错误:值无效

the error Code is : "devises: Validation Error: Value is not valid"

推荐答案

您的对象设计需要包含 equals() hashCode()方法。

Your object Devise would need to contain equals() and hashCode() methods.

你也可以使用这个泛型转换器,它可以用于所有类型的对象,你不需要为所有选择列表编写转换器。

Also you can use this generic converter which would workd for all the types of object and you won't need to write a converter for all the select lists.

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {

    private static Map<Object, String> entities = new WeakHashMap<Object, String>();

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object entity) {
        synchronized (entities) {
            if (!entities.containsKey(entity)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(entity, uuid);
                return uuid;
            } else {
                return entities.get(entity);
            }
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(uuid)) {
                return entry.getKey();
            }
        }
        return null;
    }

}

这篇关于selectOneMenu转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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