简体   繁体   中英

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).

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

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

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

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

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 RenderKit s 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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