views:

641

answers:

3

I am trying to insert line break tags
into some text and displaying it on a web page. The < and > signs are being translated into &lt; and &gt; and the tags are showing up as text on the web page.

The text looks like this when I select it from the database (I've output it to SYSOUT):

version 12.4
service timestamps debug datetime
service timestamps log datetime
service password-encryption

Then I run it through this little filter:

public DevConfigs getDevConfig() {

    String config = devConfig.getConfig();

    Pattern pattern = Pattern.compile(".$", Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = pattern.matcher(config);
    String newConfig = matcher.replaceAll("<br />");

    devConfig.setConfig(newConfig);
    return this.devConfig;
}

Here is the web page (it's a Seam application using facelets):

<rich:tab label="Config">
    hello<br />
    there<br />
    #{devConfig.config}
</rich:tab>

And the page source looks like this:

hello<br />
there<br />
&lt;br /&gt;
&lt;br /&gt;

version 12.&lt;br /&gt;
service timestamps debug datetim&lt;br /&gt;
service timestamps log datetim&lt;br /&gt;
service password-encryptio&lt;br /&gt;
&lt;br /&gt;

As you can see, my tag comes out as HTML characters and not as tags. What do I need to do to insert line break tags at the end of each line of text??

TDR

A: 

I don't write that much Java, especially nothing related to this, but I'm wondering, can this

<rich:tab label="Config">
    hello<br />
    there<br />
    #{devConfig.config}
</rich:tab>

not be written as this

<rich:tab label="Config">
    <![CDATA[
    hello<br />
    there<br />
    ]]>
    #{devConfig.config}
</rich:tab>

or simply:

<rich:tab label="Config">
    <![CDATA[
    hello
    there
    ]]>
    #{devConfig.config}
</rich:tab>

?

George Profenza
+2  A: 

You need to disable the (default) HTML escaping of text. You can do that with h:outputText (or any RichFaces equivalent for that) with the escape attribute set to false.

<h:outputText value="#{bean.property}" escape="false" />
BalusC
This was originally inside an h:outputText tag, but I didn't know about the escape attribute. I'm going to test this one out. I ended up putting a <pre> tag around the output text. Not to "hi tech", but it did work in the end.
april26
I forgot to say that I took out all the regexp matching and replacing and just displayed the code as is from the database within a <pre> tag.
april26
A: 

Maybe I didn't understand your question completely. But isn't it possible to replace simply the \n with <br />?

public DevConfigs getDevConfig() {

    String config = devConfig.getConfig();
    String newConfig = config.replace("\n", "<br />");
    devConfig.setConfig(newConfig);
    return this.devConfig;
}

I entered your text in this regex tester. This works fine, but if I choose Preg by Dialect... He replaces with &lt;br /&gt;. Only with Javascript dialect it goes correct.
So maybe that is the same problem.

Oh. When you use .$ for your regex, you remove the last char from the line (when replacing).

Martijn Courteaux