p:orderList转换器getAsObject()不调用Object.toString()

时间:2015-05-29 07:44:28

标签: jsf primefaces converter

我写了一个自定义转换器如下:

@FacesConverter(value = "orderListConverter")
public class OrderListConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {

    Object ret = null;
    if (component instanceof OrderList) {
        Object list = ((OrderList) component).getValue();
        ArrayList<ExampleEntity> al = (ArrayList<ExampleEntity>) list;
        for (Object o : al) {
            String name = "" + ((ExampleEntity) o).getName();
            if (value.equals(name)) {
                ret = o;
                break;
            }
        }
    }
    return ret;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) 
{
    String str = "";
    if (value instanceof ExampleEntity) {
        str = "" + ((ExampleEntity) value).getNumber();
    }
    return str;
}
}

我的ExampleEntity实现如下:

public class ExampleEntity {

private String name;

private int number;

public ExampleEntity(String name, int number) {
    this.name = name;
    this.number = number;
}

@Override
public String toString() {
    return "toString(): [name=" + name + ", number=" + number + "]";
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

}

来自Primefaces的orderList-Component看起来像:

<p:orderList value="#{orderListBean.exampleList}"
var="exampleEntity" itemValue="#{exampleEntity}"
converter="orderListConverter">

    <p:column style="width:25%">
        #{exampleEntity.number}
    </p:column>
    <p:column style="width:75%;">
        #{exampleEntity.name}
    </p:column>
</p:orderList>

并且bean实现如下:

@SessionScoped
@ManagedBean(name = "orderListBean")
public class OrderListBean {

private List<ExampleEntity> exampleList;

@PostConstruct
public void init() {

    exampleList = new ArrayList<ExampleEntity>();

    exampleList.add(new ExampleEntity("nameOne", 1));
    exampleList.add(new ExampleEntity("nameTwo", 2));
    exampleList.add(new ExampleEntity("nameThree", 3));
    exampleList.add(new ExampleEntity("nameFour", 4));
    exampleList.add(new ExampleEntity("nameFive", 5));

}

public List<ExampleEntity> getExampleList() {
    return exampleList;
}

public void setExampleList(List<ExampleEntity> exampleList) {
    this.exampleList = exampleList;
}

}

1)调试时,value的{​​{1}}参数包含 getAsObject()的{​​{1}},但我曾预料到。{1}} 要调用number的{​​{1}}方法!

2)ExampleEntity属性的正确内容是什么? 这是一种比配置更常见的约定吗?或者组件如何 将toString()插入ExampleEntity

时,“知道”使用整个对象

希望一切都清楚!坦克很多都有任何解释!

3 个答案:

答案 0 :(得分:2)

转换器基本上用于在两个方向上转换值:

  1. 当呈现值时,服务器到客户端。
  2. 客户端到服务器,提交值时。
  3. 在您建立的getAsString中,字符串表示形式(客户端使用的字符串表示形式)是exampleEntity的数字。这就是将作为值呈现给客户端的内容。之后,当客户端提交其值时,该值为number。要将其转换为对象(服务器)表示,请使用getAsObject作为参数调用number
    服务器可能无法使用getAsObject致电exampleEntity.toString(),因为此时它没有exampleEntity个实例,只有提交的number。< / p>

    为了说明,这应该成立:

    obj.equals(conv.getAsObject(ctx, comp, conv.getAsString(ctx, comp, obj)));
    

    getAsObjectgetAsString在输入和输出方面应该是颠倒的。

    回答你的第二个问题:这取决于你的需求。您可以说itemValue="#{exampleEntity.number}",但只有当您对exampleEntity本身不感兴趣时​​才会有意义,即在提交时您会从客户端获得number并且&#&# 39;所有你需要的服务器端逻辑。

答案 1 :(得分:-1)

 Restaurant = FactoryBot.create(:Restaurant, name: Faker::Name.name)
 post :create, params: { location: {restaurant_ids:[Restaurant.id]}, format: 'json'
 expect(response.status).to eq(200)

答案 2 :(得分:-2)

Prueba con unogenéricopara los orderList。 Este funciona a laperfección。

@Override
public Object getAsObject(final FacesContext context, final UIComponent component, final String value) {
    if (value == null) {
        return null;
    }
    return fromSelect(component, value);
}


private Object fromSelect(final UIComponent currentcomponent, final String objectString) {


    if (currentcomponent instanceof OrderList) {
        final Object items = ((OrderList) currentcomponent).getValue();
        final List<Object> elements = (List<Object>) items;
        for (final Object element : elements) {
            if (objectString.equals(serialize(element))) {
                return element;
            }
        }
    }

    return null;
}

private String serialize(final Object object) {
    if (object == null) {
        return null;
    }
    return object.getClass() + "@" + object.hashCode();
}

@Override
public String getAsString(final FacesContext arg0, final UIComponent arg1, final Object object) {
    return serialize(object);
}