SelectOneMenu中的JSF转换器问题

时间:2012-08-31 00:00:04

标签: jsf converter

再来一次我在这里遇到麻烦了。我的观点是: 在我的项目中,我需要一个转换器(显然)将SelectOneMenu组件中的项目转换为相应bean中的list属性。在我的jsf页面中,我有:

<p:selectOneMenu id="ddlPublicType" value="#{publicBean.selectedPublicType}"    effect="fade" converter="#{publicBean.conversor}" > 
    <f:selectItems value="#{publicoBean.lstPublicTypes}" var="pt" itemLabel="#{pt.label}" itemValue="#{pt.value}"></f:selectItems>
</p:selectOneMenu>

我的豆子是:

@ManagedBean(name = "publicBean")
@RequestScoped
public class PublicBean {

// Campos
private String name; // Nome do evento
private TdPublicType selectedPublicType = null;
private List<SelectItem> lstPublicTypes = null;
private static PublicTypeDAO publicTypeDao; // DAO 

static {
    publicTypeDao = new PublicTypeDAO();
}
// Construtor

public PublicoBean() {

    lstPublicTypes = new ArrayList<SelectItem>();
    List<TdPublicType> lst = publicTypeDao.consultarTodos();
    ListIterator<TdPublicType> i = lst.listIterator();
    lst.add(new SelectItem("-1","Select..."));
    while (i.hasNext()) {
        TdPublicType actual = (TdPublicType) i.next();
        lstPublicTypes.add(new SelectItem(actual.getIdPublicType(), actual.getNamePublicType()));
    }

}

// Getters e Setters

...

public Converter getConversor() {
    return new Converter() {
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            // This value parameter seems to be the value i had passed into SelectItem constructor
            TdPublicType publicType = null; // Retrieving the PublicType from Database based on ID in value parameter
            try {
                if (value.compareTo("-1") == 0 || value == null) {
                    return null;
                }
                publicType = publicTypeDao.findById(Integer.parseInt(value));
            } catch (Exception e) {
                FacesMessage msg = new FacesMessage("Error in data conversion.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage("info", msg);
            }
            return publicType;
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            return value.toString(); // The value parameter is a TdPublicType object ?
        }
    };
}

...
}

getAsObject()方法中,value参数似乎是我传递给SelectItem构造函数的值。但是在 getAsString()方法中,该值似乎也是Id的字符串表示形式。此参数不应为TdPublicType类型?我的代码有什么问题吗?

3 个答案:

答案 0 :(得分:4)

getAsString()应将Object(在您的TdPublicType类型的情况下)转换为唯一标识该实例的String,例如一些ID,以便它可以在HTML代码中内联并作为HTTP请求参数传递。 getAsObject()应该将该唯一String表示形式转换回具体的Object实例,以便将提交的HTTP请求参数转换回原始对象实例。

基本上(省略了普通的预检和异常处理):

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException {
    // Convert Object to unique String representation for display.
    return String.valueOf(((TdPublicType) modelValue).getId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) throws ConverterException {
    // Convert submitted unique String representation back to Object.
    return tdPublicTypeService.find(Long.valueOf(submittedValue));
}

更新:您还有另一个问题,即您将value类的TdPublicType属性指定为项值而不是TdPublicType实例本身。这样,转换器将检索value属性,而不是TdPublicType中的getAsString()实例。相应地修复它:

<f:selectItems value="#{publicoBean.lstPublicTypes}" var="pt" 
    itemLabel="#{pt.label}" itemValue="#{pt}"/>

答案 1 :(得分:0)

现在代码正常运行。我的错误在于加载方法。我这样做了:

// Loading menu
List<TdPublicType> l = daoPublicType.retrieveAll();
Iterator<TdPublicType> i = l.iterator();
while (i.hasNext()) {
    TdPublicType actual = (TdPublicType) i.next();
    lstMenuPublicType.add(new SelectItem(actual.getIdtPublicType(), actual.getNamePublicType()));
}

但正确的方法是:

// Loading menu
List<TdPublicType> l = daoPublicType.retrieveAll();
Iterator<TdPublicType> i = l.iterator();
while (i.hasNext()) {
    TdPublicType actual = (TdPublicType) i.next();
    lstMenuPublicType.add(new SelectItem(actual, actual.getNamePublicType())); // In the first parameter i passed the PublicType object itself not his id.
} 

答案 2 :(得分:0)

use可以使用泛型转换器,它将转换支持bean中的值。 你也不需要任何铸造。

@FacesConverter(value = "GConverter")
public class GConverter 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;
    }

}

示例用法是

<p:selectOneMenu id="ddlPublicType" value="#{publicBean.selectedPublicType}"    effect="fade" converter="GConverter" > 
    <f:selectItems value="#{publicoBean.lstPublicTypes}" var="pt" itemLabel="#{pt.label}" itemValue="#{pt}"></f:selectItems>
</p:selectOneMenu>