How can I replace newline characters using JSP and

2019-01-08 20:10发布

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 JSTL, so that the field can be displayed in a text input. I have found one solution, but it's not very elegant. I'll post below as a possibility.

14条回答
太酷不给撩
2楼-- · 2019-01-08 20:30

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, "; ")}
查看更多
Deceive 欺骗
3楼-- · 2019-01-08 20:30

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.

查看更多
甜甜的少女心
4楼-- · 2019-01-08 20:31

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 />")}
查看更多
爷、活的狠高调
5楼-- · 2019-01-08 20:32

Here is a solution I found. It doesn't seem very elegant, though:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<% pageContext.setAttribute("newLineChar", "\n"); %>

${fn:replace(item.comments, newLineChar, "; ")}
查看更多
Anthone
6楼-- · 2019-01-08 20:32

This is a valid solution for the JSP EL:

"${fn:split(string1, Character.valueOf(10))}"
查看更多
该账号已被封号
7楼-- · 2019-01-08 20:37

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)}
查看更多
登录 后发表回答