How can I replace newline characters using JSP and JSTL?

前端 未结 14 1965
遥遥无期
遥遥无期 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:11

    \n does not represent the newline character in an EL expression.

    The solution which sets a pageContext attribute to the newline character and then uses it with JSTL's fn:replace function does work.

    However, I prefer to use the Jakarta String Tab Library to solve this problem:

    <%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %>
    ...
    
    Text containing newlines
    
    ...
    

    You can use whatever you want for the newlineToken; "~n" is unlikely to show up in the text I'm doing the replacement on, so it was a reasonable choice for me.

提交回复
热议问题