How to put "new line" in JSP's Expression Language?
Asked Answered
E

5

6

What would be right EL expression in JSP to have a new line or HTML's <br/>? Here's my code that doesn't work and render with '\n' in text.

<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS}\n#{msg.TCW_SELECT_PART_ANALYSIS2}"/>
Estus answered 15/12, 2009 at 15:52 Comment(4)
You probably want to use <br/>Matlock
I added the "jsf" tag, assuming it from the <af: prefix. Remove it if I'm wrongTestes
@Bozho: it's the Oracle ADF faces, so you're right.Retardant
yes, I assumed ADF, but who knows what prefixes could one configure for custom tag libraries :)Testes
T
14

Since you want to output <br />, just do:

<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS}<br />#{msg.TCW_SELECT_PART_ANALYSIS2}" escape="false" />

The attribute escape="false" is there to avoid the <br /> being HTML-escaped.

You can even display the two messages in separate tags and put the <br /> in plain text between them.

<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS}" />
<br />
<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS2}" />

If you're still on JSF 1.1 or older, then you need to wrap plain HTML in <f:verbatim> like:

<f:verbatim><br /></f:verbatim>
Testes answered 15/12, 2009 at 15:57 Comment(1)
where do I put these <af:outputText... things? Like I have a jsp file, where do they go?Blanche
K
5

If you want a new line in the browser then you need to put "<br/>" in the text. The browser will then interpret it correctly. It does not understand \n.

Keyway answered 15/12, 2009 at 15:57 Comment(1)
This is the best answer!Blanche
P
2

How about:

<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS}"/>
<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS2}"/>

(i.e. split the value and put the character you want between the two)?

Pedrick answered 15/12, 2009 at 15:56 Comment(0)
R
1

Write a custom function that calls this piece of code:

import java.util.StringTokenizer;

public final class CRLFToHTML {

    public String process(final String text) {

        if (text == null) {
            return null;
        }

        StringBuilder html = new StringBuilder();

        StringTokenizer st = new StringTokenizer(text, "\r\n", true);

        while (st.hasMoreTokens()) {
            String token = st.nextToken();

            if (token.equals("\n")) {
                html.append("<br/>");
            } else if (token.equals("\r")) {    
                // Do nothing    
            } else {    
                html.append(token);    
            }
        }

        return html.toString();

    }

}
Rhesus answered 15/12, 2009 at 18:51 Comment(1)
This exist already in flavor of JSTL fn:replace. Besides, that's also not needed if you use escape="false" in UIOutput as pointed by Bozho.Retardant
R
0

use </br> instead of /n

ie. inside double quotes "" its allowed to use </br>

eg.

<af:outputText value="#{msg.TCW_SELECT_PART_ANALYSIS}</br>{msg.TCW_SELECT_PART_ANALYSIS2}"/> 
Rogerrogerio answered 30/6, 2021 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.