How can I replace newline characters using JSP and JSTL?

前端 未结 14 1904
遥遥无期
遥遥无期 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 23:18

    This solution is more elegant than your own solution which is setting the pagecontext attribute directly. You should use the <c:set> tag for this:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <c:set var="newLine" value="\n"/>
    ${fn:replace(data, newLine, "; ")}
    

    BTW: ${fn:replace(data, "\n", ";")} does NOT work.

    0 讨论(0)
  • 2020-12-02 23:18

    In the value while setting the var, press ENTER between the double quotes.

    ${fn:replace(data, newLineChar, ";")}

    0 讨论(0)
提交回复
热议问题