SelectOneMenu中的JSF Converter问题 [英] JSF Converter issue in SelectOneMenu

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

问题描述

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

one more time i'm in trouble here. My point is: In my project i need a converter for (obviously) convert the items from the SelectOneMenu component to a list property in the respective bean. In my jsf page i have:

<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类型吗?我的代码有什么问题吗?

In the getAsObject() method, the value parameter seems to be the value i had passed into SelectItem constructor. But in the getAsString() method, the value also seems to be a string representation of an Id. This parameter shouldn't be of type TdPublicType ? There is anything wrong in my code?

推荐答案

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

The getAsString() should convert the Object (which is in your case of type TdPublicType) to a String which uniquely identifies the instance, e.g. some ID, so that it can be inlined in HTML code and passed around as HTTP request parameters. The getAsObject() should convert exactly that unique String representation back to the concrete Object instance, so that the submitted HTTP request parameter can be converted back to the original object instance.

基本上(省去了简单的预检查和异常处理):

Basically (trivial prechecks and exception handling omitted):

@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));
}






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


Update: you've another problem, you're specifying the value property of TdPublicType class as the item value instead of the TdPublicType instance itself. This way the converter will retrieve the value property instead of the TdPublicType instance in the getAsString(). Fix it accordingly:

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

这篇关于SelectOneMenu中的JSF Converter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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