How can I replace newline characters using JSP and JSTL?

前端 未结 14 1902
遥遥无期
遥遥无期 2020-12-02 22:15

I have a list of bean objects passed into my JSP page, and one of them is a comment field. This field may contain newlines, and I want to replace them with semicolons using

相关标签:
14条回答
  • 2020-12-02 22:53

    For the record, I came across this post while tackling this problem:

    A multi-line string in JSTL gets added as the title attribute of a textarea. Javascript then adds this as the default text of the textarea. In order to clear this text on focus the value needs to equal the title... but fails as many text-editors put \r\n instead of \n. So the follownig will get rid of the unwanted \r:

    <% pageContext.setAttribute("newLineChar", "\r"); %> 
    <c:set var="textAreaDefault" value="${fn:replace(textAreaDefault, newLineChar, '')}" />
    
    0 讨论(0)
  • 2020-12-02 22:54

    If what you really need is a \n symbol you can use the advice from here:

    ${fn:replace(text, "
    ", "<br/>")}
    

    or

    <c:set var="nl" value="
    " /><%-- this is a new line --%>
    

    This includes the new line in your string literal.

    0 讨论(0)
  • 2020-12-02 22:55

    You could write your own JSP function to do the replacement.

    This means you'd end up with something like:

    <%@ taglib prefix="ns" uri="..." %>
    ...
    ${ns:replace(data)}
    

    Where ns is a namespace prefix you define and replace is your JSP function.

    These functions are pretty easy to implement (they're just a static method) although I can't seem to find a good reference for writing these at the moment.

    0 讨论(0)
  • 2020-12-02 22:56

    You could create your own JSP function. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags6.html

    This is roughly what you need to do.

    Create a tag library descriptor file
    /src/META-INF/sf.tld

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
      <tlib-version>1.0</tlib-version>
      <short-name>sf</short-name>
      <uri>http://www.stackoverflow.com</uri>
      <function>
        <name>clean</name>
        <function-class>com.stackoverflow.web.tag.function.TagUtils</function-class>
        <function-signature>
          java.lang.String clean(java.lang.String)
        </function-signature>
      </function>
    </taglib>
    

    Create a Java class for the functions logic.
    com.stackoverflow.web.tag.function.TagUtils

    package com.stackoverflow.web.tag.function;
    
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class TagUtils extends TagSupport {
      public static String clean(String comment) {
        return comment.replaceAll("\n", "; ");
      }
    }
    

    In your JSP you can access your function in the following way.

    <%@ taglib prefix="sf" uri="http://www.stackoverflow.com"%>
    ${sf:clean(item.comments)}
    
    0 讨论(0)
  • 2020-12-02 22:57

    This is similar to the accepted answer (because it is using Java to represent the newline rather than EL) but here the <c:set/> element is used to set the attribute:

    <c:set var="newline" value="<%= \"\n\" %>" />
    ${fn:replace(myAddress, newline, "<br />")}
    

    The following snippet also works, but the second line of the <c:set/> element cannot be indented (and may look uglier):

        <c:set var="newline" value="
    " /><!--this line can't be indented -->
        ${fn:replace(myAddress, newline, "<br />")}
    
    0 讨论(0)
  • 2020-12-02 23:04

    This does not work for me:

    <c:set var="newline" value="\n"/>
    ${fn:replace(data, newLine, "; ")}
    

    This does:

    <% pageContext.setAttribute("newLineChar", "\n"); %> 
    ${fn:replace(item.comments, newLineChar, "; ")}
    
    0 讨论(0)
提交回复
热议问题