h:outputText 似乎修剪了空格,如何保留空格? [英] h:outputText seems to trim whitespace, how do I preserve whitespace?

查看:27
本文介绍了h:outputText 似乎修剪了空格,如何保留空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,可以动态查询数据库以在页面上显示数据,类似于 Toad 等数据库工具(当然甚至不关闭:),示例仅用于说明).

I have a web page that queries database dynamically to display data on the page, similar to database tool like Toad etc. (not even close of course :), example for illustration only).

问题是数据在页面上显示时被修剪.这就是我使用 JSF 显示数据的方式

The problem is data gets trimmed when displayed on the page. This is how I display data using JSF

<h:outputText value="#{record[columnIndex].toDisplayString()}" />

我相信它是关于 html 渲染的.我该怎么办?写一个html编码器?如何?非常感谢您的帮助.

I believe it is about html rendering. What should I do? Write an html encoder? How? Help would be highly appreciated.

推荐答案

根本不修剪值.

也许您正在谈论值中的空格,如前导/尾随空格、制表符、换行符、回车符等,默认情况下,它们在 HTML 标记中完全没有意义.它只是成为 HTML 源代码的一部分,而不是 HTML 表示.例如,换行符在 HTML 中由 <br> 元素表示,而不是由 字符表示.

Perhaps you're talking about whitespace like leading/trailing spaces, tabs, newlines, carriage returns, etc in the value, which have by default totally no meaning in HTML markup. It just becomes part of the HTML source code, but not the HTML presentation. Newlines, for example, are in HTML to be represented by the <br> element, not by the character.

如果您想在 HTML 源代码中保留 HTML 元素节点中的空格,那么您需要设置父 HTML 元素的 CSS white-space 属性到 pre 以保留它.如果您想在块元素中换行,请使用 pre-wrap.

If you'd like to preserve the whitespace in a HTML element node as it is in the HTML source code, then you need to set the parent HTML element's CSS white-space property to pre in order to preserve it. If you'd like to wrap lines in block elements, then use pre-wrap.

例如

<h:outputText ... styleClass="preformatted" />

.preformatted {
    white-space: pre-wrap;
}

另一种方法是自己将文本转换为有效的 HTML 标记.例如.用 <br/> 字符串替换每个出现的 字符.您可以为此使用 EL 函数.

An alternative is to convert the text to valid HTML markup yourself. E.g. replacing every occurrence of character by the <br/> string. You could use an EL function for this.

这篇关于h:outputText 似乎修剪了空格,如何保留空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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