Replace "\n" in EL
Asked Answered
C

3

7

In a JSP file I wanto to replace newlines (\n) with <br />. I tried

${fn:replace(someString, '\n', '<br />')}

But I get an error '\n' ecnountered, was expeting one of...

Which I guess it means the parser doesn't like something like this.

Is it possible to do something like this using EL?

Conformal answered 3/12, 2012 at 16:3 Comment(2)
Found workaround <% pageContext.setAttribute("newLine", "\n"); %> ${fn:replace(someString, newLine, '<br />')}Conformal
Sorry, please look at my edited demo.Antepast
C
10

Create an EL function for that.

First create a static method which does the desired job:

package com.example;

public final class Functions {
     private Functions() {}

     public static String nl2br(String string) {
         return (string != null) ? string.replace("\n", "<br/>") : null;
     }
}

Then create a /WEB-INF/functions.tld which look like follows:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>Custom_Functions</short-name>
    <uri>http://example.com/functions</uri>

    <function>
        <name>nl2br</name>
        <function-class>com.example.Functions</function-class>
        <function-signature>java.lang.String nl2br(java.lang.String)</function-signature>
    </function>
</taglib>

Finally use it as follows:

<%@taglib uri="http://example.com/functions" prefix="f" %>
...
${f:nl2br(someString)}

See also:

Colver answered 13/12, 2012 at 16:23 Comment(6)
I tried to use with <c:set var="test" value="${f:nl2br(someString)}"/> but didn't work. How can i do this suppose i need to use <c:set.. ?Euromarket
@Doni: Just press "Ask Question" and post an SSCCE.Colver
I have used escapeXml=false otherwise it print to screen still the <br/>...example: <c:out escapeXml="false" value="${f:nl2br(someString)}"/>Voe
@user3410465: just don't use <c:out> at all. Simply do ${f:nl2br(someString)}. This is only not supported in JSP 1.x, but as JSP 2.0 was released more than a decade ago, no one would expect one being still on JSP 1.x these days.Colver
@Colver Be aware that you risk xss vulnerabilities without <c:out>, if for instance someString is a user input. I suggest processing the string with <c:out> and then passing the result to f:nlbr().Fruge
@fishbone: That's correct. See also https://mcmap.net/q/17823/-xss-prevention-in-jsp-servlet-web-application for better alternatives in case this is really user-controlled input.Colver
A
2

Your way is a lot easier. Why didn't I think of that. Here is a demo page.

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="newLine" value="\n" />
<c:set var="myText" value="one\ntwo\nthree" />
${fn:replace(myText, newLine, '<br/>')}
Antepast answered 3/12, 2012 at 17:25 Comment(1)
Thanks for the workaround, but this is not good, it will need to URL decode it before printing, otherwise it would print %XX instead of spaces, accented letters etc.Conformal
M
0

For me, it worked by first defining a variable newLine, either with

<% pageContext.setAttribute("newLine", "\n"); %>

Or

<c:set var="newLine" value="<%= '\n' %>" />

And then

${fn:replace(someString, newLine, '<br/>')}
Maracanda answered 5/7, 2017 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.