Primefaces orderlist自定义转换器在ajax后面提交错误的字符串表示

时间:2018-05-17 14:33:46

标签: primefaces jsf-2.2

我需要重新调整列表中某些数据模型的顺序,因此我使用Primefaces orderlist。小面孔是:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form id="main-form">
        <p:panelGrid id="grid" columns="3">
            <p:outputLabel value="Name" for="label"/> 
            <p:inputText id="label" value="#{sampleBean.obj.name}" />
            <p:message for="label"/>
        </p:panelGrid>
        <br/><p:commandButton value="Add" action="#{sampleBean.addToList()}" update="@form" />
        <br/>
        <p:orderList id="rows" value="#{sampleBean.list}" converter="sample2" 
                     itemValue="#{row}" var="row">
            <p:column>
                #{row.name}
            </p:column>
        </p:orderList>
        <br/><p:commandButton value="Export" action="#{sampleBean.export()}" update="@form" />
    </h:form>
    <p:messages autoUpdate="true"/>
</h:body>
</html>

支持bean是:

@javax.inject.Named
@javax.faces.view.ViewScoped
public class SampleBean implements java.io.Serializable {
private List<SampleModel> list;
private SampleModel obj;

public SampleBean() {
    list = new ArrayList<>();
    obj = new SampleModel();
    obj.setName("");
}

public void addToList() {
    list.add(obj);
    obj = new SampleModel();
    obj.setName("");
}

public void export() {
    StringBuilder buf= new StringBuilder();
    for (SampleModel m: list) {
        buf.append(m.getName()).append(',');
    }
    FacesMessage msg = new FacesMessage(buf.toString());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public List<SampleModel> getList() {
    return list;
}

public void setList(List<SampleModel> list) {
    this.list = list;
}

public SampleModel getObj() {
    return obj;
}

public void setObj(SampleModel obj) {
    this.obj = obj;
}
}

样本模型是:

public class SampleModel {

private String name;
private String uname;

public String getName() {
    return name;
}

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

public String getUname() {
    return uname;
}

public void setUname(String uname) {
    this.uname = uname;
}
}

使用简单的转换器就可以了。但由于我的实际用法具有灵活的结构,因此该对象将成为org.bson.Document。以下使用JSON表示的转换器不起作用:

@FacesConverter("sample2")
public class Sample2Converter  implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    SampleModel model = new SampleModel();
    try {
        Document d = Document.parse(value);
        model.setName(d.getString("name"));
    }
    catch (RuntimeException ex) {
        model.setName("exception");
        Logger.getLogger("Sample2Converter").log(Level.SEVERE, null, ex);
    }
    return model;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value instanceof SampleModel) {
        SampleModel model = (SampleModel)value;
        return new Document().append("name", model.getName()).append("uname", model.getUname()).toJson();
    }
    else
        return "invalid";
}

}

从Chrome浏览器调试,在ajax回发后,列表正在提交 main-form:rows_values:[object Object] 代替 main-form:rows_values:{&#34; name&#34;:&#34; apple&#34;}

我正在使用Mojarra 2.2.12,Primefaces 6.1。

1 个答案:

答案 0 :(得分:0)

转换为json会欺骗Primefaces脚本。作为一种解决方法,我在自定义转换器中使用base64编码/解码并解决了这个问题。