user745235
user745235

Reputation:

JSF Converter changing option value

I have a Converter and it is changing the values of all my options:

Whe I first create the selectOneMenu I have this 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>

When I submit my form, it will call the converter and convert it to object, it is making some kind of mess with my values (ids), it is mostly repeating the same id on the first and last results.

<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>

Here is my 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;
    }
}

I have noticed something but I don't know if it is the correct behavior for a converter, every time the getAsObject is called, at the end, it calls getAsString again.

Upvotes: 1

Views: 574

Answers (1)

BalusC
BalusC

Reputation: 1109715

Your productSubgroupVO is declared static and thus shared between all converter instances and threads. This is not threadsafe. Remove it and declare it in the method local scope only.

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

Upvotes: 1

Related Questions