0

在测试期间,如果带有 JSON 数据的 HTTP 响应包含“&”,primefaces 会失败,如果我删除“&”它可以工作。我发送的数据是一个 URL,我需要以编码格式发送 url 吗?如果任何数据(不是 url)包含“amp”或任何类型的此类字符,是否会发生此问题?在这种情况下,最好的方法是什么。

带有回调的命令链接

<h:form prependId="false" id="getAllTopics" style="display:none;">
        <p:commandLink action="#{topicController.listAllTopics}" id="topicListAllCmdLink" value=""
            oncomplete="javascript:renderTopic(xhr, status,args)" />
    </h:form>

JS函数

function renderTopic(xhr,status,args){
        jsonArray=[];
        console.log(args.topicJSON);
        jsonArray = $.parseJSON(args.topicJSON);
   .....
}

来自 Google 开发者工具包的响应

<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[4297043168621098325:5437886159631978839]]></update><extension ln="primefaces" type="args">{"topicJSON":"[{\"topicBody\":\"http://www.youtube.com/watch?v=m3ZyU98N3Fk&feature=relmfu \",\"videoAudioUrl\":\"\",\"topicGuid\":5600,\"userGuid\":0,\"imageURL\":\"\",\"topicOwnUserName\":\"srikanth marni\",\"topicCommentList\":[],\"topicUpdateTime\":1346817736000}]"}</extension></changes></partial-response>

控制台错误

Uncaught TypeError: Cannot read property 'topicJSON' of undefined circle_topic.js.jsf:150
renderTopic circle_topic.js.jsf:150
PrimeFaces.ab.oncomplete circle.jsf:175
k.complete primefaces.js.jsf:1
b.Callbacks.bv jquery.js.jsf:16
b.Callbacks.bE.fireWith jquery.js.jsf:16
bF jquery.js.jsf:23
b.ajaxTransport.send.bv
4

1 回答 1

0

目前(v.5.2)primefaces 部分响应编写器不会为 XML 转义 JSON,这会导致字符 '<'、'>'、'&' 等出现问题。

为了让它工作,我修补了 org.primefaces.context.PrimePartialResponseWriter 中的 encodeCallbackParams 方法,如下所示,而 XML 是 org.json.XML:

private void encodeCallbackParams(Map<String, Object> params) throws IOException, JSONException {

if (params != null && !params.isEmpty()) {

    startExtension(CALLBACK_EXTENSION_PARAMS);
    write("{");

    for (Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
    String paramName = it.next();
    Object paramValue = params.get(paramName);

    if (paramValue instanceof JSONObject) {
        String json = ((JSONObject) paramValue).toString();
        json = XML.escape(json);
        write("\"");
        write(paramName);
        write("\":{");
        write(json.substring(1, json.length() - 1));
        write("}");
    } else if (paramValue instanceof JSONArray) {
        String json = ((JSONArray) paramValue).toString();
        json = XML.escape(json);
        write("\"");
        write(paramName);
        write("\":[");
        write(json.substring(1, json.length() - 1));
        write("]");
    } else if (isBean(paramValue)) {
        String json = new JSONObject(paramValue).toString();
        json = XML.escape(json);
        write("\"");
        write(paramName);
        write("\":");
        write(json);
    } else {
        String json = new JSONObject().put(paramName, paramValue).toString();
        json = XML.escape(json);
        write(json.substring(1, json.length() - 1));
    }

    if (it.hasNext()) {
        write(",");
    }
    }

    write("}");
    endExtension();
}
}
于 2015-04-17T09:54:32.237 回答