Primefaces订单清单自定义转换器在Ajax上提交错误的字符串表示形式 [英] Primefaces orderlist custom converter submitting wrong string representation on ajax post back

查看:67
本文介绍了Primefaces订单清单自定义转换器在Ajax上提交错误的字符串表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重新排列列表中某些数据模型的顺序,因此我使用Primefaces顺序表.该方面是:

I need to reshuffle the order of some data models in a list, so I use Primefaces orderlist. The facelet is:

<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表示形式的转换器不起作用:

Using a simple converter is OK. But since my real usage has a flexible structure, so the object will gonna be org.bson.Document. The following converter using a JSON representation does not work:

@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:[对象对象] 代替 main-form:rows_values:{名称":苹果"}

From Chrome browser debug, upon ajax post back, the list is submitting main-form:rows_values: [object Object] instead of main-form:rows_values: {"name":"apple"}

我正在使用Mojarra 2.2.12,Primefaces 6.1.

I am using Mojarra 2.2.12, Primefaces 6.1.

推荐答案

转换为json会使Primefaces脚本变得愚蠢.解决方法是,在自定义转换器中使用base64编码/解码,并解决了该问题.

Conversion to json fools the Primefaces script. As a work around, I use base64 encode/decode in the custom converter and solved the problem.

这篇关于Primefaces订单清单自定义转换器在Ajax上提交错误的字符串表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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