使用escape =“false”和值更新h:outputText,其中包含'\ u001c'

时间:2015-12-17 16:09:10

标签: jsf

我有以下Java代码:

@Named
@SessionScoped
public class TestClass implements Serializable {

    private String stringValue;

    @PostConstruct
    private void init() {
        initStringValueDefault();
    }

    public void initStringValue1() {
        stringValue = "ABCD";
    }

    public void initStringValue2() {
        stringValue = "EFGH" + '\u001c';
    }

    public void initStringValueDefault() {
        stringValue = "LABEL TEXT";
    }

    public String getStringValue() {
        return stringValue;
    }
}

和.xhtml

<h:form id="testForm" prependId="true">
    <h:outputText id="stringValueLabel" value="#{testClass.stringValue}" escape="false"/>
    <br/>
    <h:commandButton value="Set ABCD" actionListener="#{testClass.initStringValue1()}">
        <f:ajax execute="@form" render="stringValueLabel"/>
    </h:commandButton>
    <span style="margin-left: 10px"/>
    <h:commandButton value="Set EFGH + \u001c" actionListener="#{testClass.initStringValue2()}">
        <f:ajax execute="@form" render="stringValueLabel"/>
    </h:commandButton>
    <span style="margin-left: 10px"/>
    <h:commandButton value="Set default" actionListener="#{testClass.initStringValueDefault()}">
        <f:ajax execute="@form" render="stringValueLabel"/>
    </h:commandButton>
</h:form>

当我点击“设置ABCD”按钮时,更新可以成功运行。但点击'设置EFGH + \ u001c'后我收到错误:

  1. Chrome:'localhost:8080页面显示:emptyResponse:从服务器收到空响应。'
  2. Firefox:'malformedXML:XML解析错误:格式不正确'
  3. 并且标签尚未更新。页面重新加载后,标签更新为'EFGH'。

    有人知道,为什么由于在变量中添加'\ u001c',按钮点击后更新不起作用?

    使用escape =“true”一切正常,但我需要escape =“false”。

1 个答案:

答案 0 :(得分:0)

通过使用org.apache.commons.lang3.StringEscapeUtils的方法escapeXml11(String input)处理变量来解决问题:

import static org.apache.commons.lang3.StringEscapeUtils.escapeXml11;
...
    public void initStringValue2() {
        stringValue = "EFGH" + '\u001c';
        stringValue = escapeXml11(stringValue);
    }
...
相关问题