JSF列表转换器 [英] JSF List Converter

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

问题描述

如何在JSF2中为A类项目列表编写转换器?我已经为A类编写了一个转换器,但这些项目使用默认的toString()函数显示:A @ hashcode。

How do I write a converter for a list of items of class A in JSF2? I have written a converter for class A, but the items show up using the default toString() function: "A@hashcode".

我需要使用转换器而不是而不是支持bean方法,以便可以进行验证(Hibernate Validator)。

I need to use a converter rather than a backing bean method so that validation can take place (Hibernate Validator).

这是我如何使用该列表:

This is how I use the list:

<h:inputText id="destinations" value="#{rule.destinations}" converter="gr.panayk.vinyls.Destination"/>

其中#{rule.destinations}是列表< Destination> 类型。我期待以逗号分隔的已转换目的地列表。

Where #{rule.destinations} is of List<Destination> type. I am expecting a comma separated list of converted Destinations.

我附加了BalusC建议的列表转换器。

I attach the List converter that BalusC proposed.

@FacesConverter(value="gr.panayk.vinyls.converter.DestinationList")
public class DestinationListConverter implements Converter
{
    @Override
    public Object getAsObject(final FacesContext context, final UIComponent component, final String values)
    {
        final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        final List<Destination> result = new ArrayList<Destination>(); 
        for (String value : values.split(",", -1))
        {           
            final String trimmedValue = value.trim();

            final Set<ConstraintViolation<Destination>> violations = validator.validateValue(Destination.class, "data", trimmedValue);
            if (!violations.isEmpty())
            {
                throw new ConverterException(new FacesMessage(violations.iterator().next().getMessage()));
            }

            result.add(new Destination(trimmedValue));
        }       

        final Set<ConstraintViolation<Rule>> violations = validator.validateValue(Rule.class, "destinations", result);
        if (!violations.isEmpty())
        {
            throw new ConverterException(new FacesMessage(violations.iterator().next().getMessage()));
        }       

        return result;
    }

    @Override
    public String getAsString(final FacesContext context, final UIComponent component, final Object value)
    {
        if (value instanceof List<?>)
        {
            final StringBuffer result = new StringBuffer();

            final List<?> list = (List<?>) value;

            for (int i = 0; i < list.size()-1; i++)
            {               
                if (list.get(i) instanceof Destination)
                {
                    result.append(((Destination) list.get(i)).getData());
                    result.append(", ");
                }
                else
                {
                    throw new IllegalArgumentException( "Cannot convert " + value + " object to Destination in DestinationConverter." );
                }
            }

            if (!list.isEmpty())
            {
                if (list.get(list.size()-1) instanceof Destination)
                {
                    result.append(((Destination) list.get(list.size()-1)).getData());
                }
                else
                {
                    throw new IllegalArgumentException( "Cannot convert " + value + " object to Destination in DestinationConverter." );
                }
            }

            return result.toString();
        }
        else
        {
            throw new IllegalArgumentException( "Cannot convert " + value + " object to List in DestinationConverter." );
        }
    }
}


推荐答案


我为A类编写了一个转换器,但这些项目使用默认的toString()函数显示:@ hashcode。

如果你没有在组件上明确声明转换器,就会发生这种情况。例如< h:selectManyCheckbox> < h:selectManyListbox> 明确声明转换器是必需的JSF / EL知道该值的类型是 List ,而不是 List< A> (泛型类型在运行)。如果您没有声明转换器,那么这些值将被视为 String (因为这是HTML输出和HTTP请求参数值的默认值)。

That can happen if you didn't explicitly declare the converter on the component. In for example <h:selectManyCheckbox> and <h:selectManyListbox> explicitly declaring the converter is mandatory as all JSF/EL knows is that the value is of type List, not List<A> (generic types are lost during runtime). If you don't declare a converter, then the values will be treated as String (as that's what HTML output and HTTP request parameter values default to).

例如

<h:selectManyCheckbox converter="aConverter">

with

@FacesConverter(value="aConverter", forClass=A.class)
public class AConverter implements Converter {

    // ...

}

当您使用<$ $等单项输入时,无需明确声明上述转换器c $ c>< h:selectOneMenu> 因为 forClass 无论如何都会匹配它。

Explicitly declaring the above converter is not necessary when you're using single-item inputs like <h:selectOneMenu> as the forClass would match it anyway.

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

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