views:

677

answers:

4

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

+1  A: 

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

Will Hartung
Will that translate into \n?
+1  A: 

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.

Bozho
Eh, partially true. While it may be designed for that purpose, it outputs text. I could write my own tag(s) that will either output a newline (\n) or a break <br/> depending on if it is outputting plain text or html. That will probably be the best and I don't need to pass any parameters since I will automatically know the output format (html or plain text) as it will be passed in I think with the response writer. This will allow me to use the same tags throughout my html markup and plain text markup content.
@Bozho - while true of the default render kit, that can be changed to suit the view technology
McDowell
yes, that's why I mentioned "renderers". But I think this would be a wrong approach for this task.
Bozho
@Bozho - ugh! - sorry, I missed your point.
McDowell
I updated it to make it more clear - it wasn't clear enough.
Bozho
A: 

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

A: 

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.

McDowell