开发者

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. I have an HTML version which works great, I hacked it together using <br/> (I'm not proud of that, but it works).

开发者_JAVA技巧

I would like to do the same for the plain text version such as inserting \n.

I am doing something like this:

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

Instead of the <br/>, I want \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


If you use Facelets to render HTML, this did the trick for me:

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


Why not simply wrap it in a HTML <pre> tag?


The h: prefix means html. So if you don't want html, don't use h: tags. Create your own tags or at least renderers for h: tags and let them output \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. 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}" />). 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 RenderKits which would provide a Renderer appropriate for the current markup. This would be the way to serve content using JSF's model-view-presenter design.

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. 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
or a \n. In my tag library, it would look like this:

<content:newline/>

Walter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜