1

我有一个转换器,它正在改变我所有选项的值:

当我第一次创建时,selectOneMenu我有这个html

<select name="j_idt14:j_idt20" size="1">
        <option value="2" selected="selected">Infantil</option> //value="2"
    <option value="6">Lançamento Inverno</option>
    <option value="5">Lançamento Verão 2</option>
    <option value="4">Lançamento Preview 1</option>
    <option value="3">Feminina</option>
    <option value="7">Masculina</option>//value="7"
</select>

当我提交表单时,它会调用转换器并将其转换为对象,它会对我的值(id)造成某种混乱,它id在第一个和最后一个结果上大多重复相同。

<select name="j_idt14:j_idt20" size="1">
    <option value="2" selected="selected">Infantil</option>//value="2"
    <option value="6">Lançamento Inverno</option>
    <option value="5">Lançamento Verão 2</option>
    <option value="4">Lançamento Preview 1</option>
    <option value="3">Feminina</option>
    <option value="2">Masculina</option>//value="2" ?
</select>

这是我的converter

@FacesConverter(value = "SubGroup")
public class SubGroup implements Converter
{
    private static ProductSubgroupVO productSubgroupVO   = new ProductSubgroupVO();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        productSubgroupVO.setId(Integer.valueOf(value));
        productSubgroupVO = (ProductSubgroupVO) new ProductSubgroupBO().getProductSubgroup(productSubgroupVO).toArray()[0];
        return (ProductSubgroupVO) productSubgroupVO;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        productSubgroupVO = (ProductSubgroupVO) value;
        String teste = String.valueOf(productSubgroupVO.getId());
        return teste;
    }
}

我注意到了一些事情,但我不知道它是否是 a 的正确行为converter,每次getAsObject调用时,最后都会getAsString再次调用。

4

1 回答 1

1

productSubgroupVO已声明并因此在所有转换器实例和线程static之间共享。这不是线程安全的。删除它并仅在方法本地范围内声明它。

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
    ProductSubgroupVO productSubgroupVO = new ProductSubgroupVO();
    productSubgroupVO.setId(Integer.valueOf(value));
    return (ProductSubgroupVO) new ProductSubgroupBO().getProductSubgroup(productSubgroupVO).toArray()[0]
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
    ProductSubgroupVO productSubgroupVO = (ProductSubgroupVO) value;
    return String.valueOf(productSubgroupVO.getId());
}
于 2012-05-16T21:14:31.727 回答