【问题标题】:JSF converter is not displaying converted valueJSF 转换器不显示转换后的值
【发布时间】:2013-05-10 11:41:15
【问题描述】:

我正在使用 internet-explorer 8 和 jsf。 我制作了我的自定义转换器,在两个空格之后添加了“\n”(以打破太长的字符串)。转换器正在被调用并返回正确的值(我用调试器检查它)但不幸的是这个正确的值没有显示在页面上。这是xhtml代码:

<h:commandLink action="#{bean.sort(property)}"
                    style="margin-left:0.01px;margin-right:0.01px;white-space:nowrap;">
                    <h:outputText value="#{header}">
                      <f:converter converterId="headerConverter" />
                    </h:outputText>
</h:commandLink>

转换器代码:

public class HeaderConverter implements Converter {

public HeaderConverter() {
}

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    /* Converter tylko do wyświetlania */
    throw new RuntimeException("HeaderConverter - display only");
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value instanceof String) {

        int i = 0;
        int spaceIter = 0;
        String header = (String) value;
        String afterChange = header;

        for (char c : header.toCharArray()) {
            if (Character.isWhitespace(c)) {
               spaceIter++;
               if(spaceIter == 2) {
                   afterChange = "" + header.substring(0, i) + "\n" + header.substring(i+1);
               }
            }
            i++;
        }
        return afterChange;
    }
    return null;
}

} 当然,我已经在 faces-config.xml 上配置了所有内容 提前感谢您的所有努力。

【问题讨论】:

    标签: internet-explorer jsf jsf-2


    【解决方案1】:

    JSF 在这个问题的上下文中仅仅是一个 HTML 代码生成器。在 HTML 中,换行符由 &lt;br&gt; 元素表示,而不是由 \n 字符表示。

    如果您在 webbrowser 中右键单击页面并执行 查看源代码,那么您会看到 \n 实际上已进入生成的 HTML 输出,但根本不参与 HTML 演示/格式化。

    所以,你有两个选择:

    1. 使用&lt;br&gt; 而不是\n

      ... + "<br/>" + ...
      

      您只需要关闭 XML 转义即可。

      <h:outputText ... escape="false" />
      

      当您重新显示用户控制的输入时,请注意潜在的 XSS 攻击漏洞!

    2. 指示 webbrowser 将 \n 解释为格式化的一部分,而不是标记的一部分(默认)。您可以为此使用 CSS white-space: pre

      style="white-space: pre;" 
      

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多