简体   繁体   English

如何在JSF(纯文本)中获取换行符?

[英]How to get a newline in JSF (plain text)?

I am using JSF to generate text and need newlines to make the text easier to read. 我正在使用JSF生成文本,需要换行符以使文本更易于阅读。 I have an HTML version which works great, I hacked it together using <br/> (I'm not proud of that, but it works). 我有一个非常好的HTML版本,我使用<br/>一起黑了它(我并不为此感到骄傲,但它有效)。

I would like to do the same for the plain text version such as inserting \\n . 我想对纯文本版本执行相同的操作,例如插入\\n

I am doing something like this: 我正在做这样的事情:

<customTagLibrary:customTag>
  <h:outputText value="Exception"/><br/><br/>
  ...
</customTagLibrary:customTag>

Instead of the <br/> , I want \\n . 取而代之的是的<br/> ,我要\\n What is the best way to do that? 最好的方法是什么?

Please keep in mind that I'm NOT using this to generate content that will be sent to the browser. 请记住,我不是用它来生成将发送到浏览器的内容。 This will be used to create email messages or (plain-text) attachments in emails. 这将用于在电子邮件中创建电子邮件或(纯文本)附件。

Thanks, 谢谢,

Walter 沃尔特

如果你使用Facelets来呈现HTML,这对我有用:

<h:outputText value="&#10;" />

为什么不简单地将其包装在HTML <pre>标签中?

The h: prefix means html. h:前缀表示html。 So if you don't want html, don't use h: tags. 因此,如果您不想要html,请不要使用h:标签。 Create your own tags or at least renderers for h: tags and let them output \\n . h:标签创建自己的标签或至少渲染器并让它们输出\\n

But my personal opinion is that it's better to use another templating technology for emails. 但我个人认为,最好将另一种模板技术用于电子邮件。

I'm assuming that your template XML strips whitespace. 我假设你的模板XML剥离了空格。 Unfortunately, EL doesn't let you express newlines in string literals, but you could bind to a string that did ( <h:outputText value="#{applicationScope.foo.newline}" /> ). 不幸的是,EL不允许你在字符串文字中表达换行符,但你可以绑定到一个字符串( <h:outputText value="#{applicationScope.foo.newline}" /> )。 However, since you want to serve multiple markups, this would be a less than ideal approach. 但是,由于您希望提供多个标记,这将是一种不太理想的方法。

To share JSF templates between different content types, you could 1) remove all markup specific tags from the template and 2) provide RenderKit s which would provide a Renderer appropriate for the current markup. 要在不同的内容类型之间共享JSF模板,您可以1)从模板中删除所有标记特定的标记,2)提供RenderKit ,它将提供适合当前标记的Renderer This would be the way to serve content using JSF's model-view-presenter design. 这将是使用JSF的模型 - 视图 - 展示器设计提供内容的方式。

You may have to make some decisions about how you handle markup-specific attributes. 您可能必须就如何处理特定于标记的属性做出一些决定。 The default render kit is geared towards rendering the HTML concrete components . 默认渲染工具包适用于呈现HTML具体组件 Exactly what you do depends on your goals. 你究竟做什么取决于你的目标。

I am going to simply write a newline tag. 我将简单地写一个换行标记。 It will detect whether it should output a 它会检测是否应输出a
or a \\n. 或者\\ n。 In my tag library, it would look like this: 在我的标签库中,它看起来像这样:

<content:newline/>

Walter 沃尔特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM