string 如何使用 JSP 和 JSTL 替换换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I replace newline characters using JSP and JSTL?
提问by parkerfath
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.
我有一个传递到我的 JSP 页面的 bean 对象列表,其中之一是注释字段。该字段可能包含换行符,我想使用 JSTL 将它们替换为分号,以便该字段可以在文本输入中显示。我找到了一种解决方案,但它不是很优雅。我会在下面发布作为一种可能性。
回答by parkerfath
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, "; ")}
回答by BalusC
Just use fn:replace()
function to replace \n
by ;
.
只需使用fn:replace()
函数替换\n
为;
。
${fn:replace(data, '\n', ';')}
In case you're using Apache's EL implementation instead of Oracle's EL reference implementation (i.e. when you're using Tomcat, TomEE, JBoss, etc instead of GlassFish, Payara, WildFly, WebSphere, etc), then you need to re-escape the backslash.
如果您使用 Apache 的 EL 实现而不是 Oracle 的 EL 参考实现(即当您使用 Tomcat、TomEE、JBoss 等而不是 GlassFish、Payara、WildFly、WebSphere 等时),那么您需要重新转义反斜杠。
${fn:replace(data, '\n', ';')}
回答by andy
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:
这类似于已接受的答案(因为它使用 Java 来表示换行符而不是 EL),但这里使用 <c:set/> 元素来设置属性:
<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/> 元素的第二行不能缩进(并且可能看起来更丑):
<c:set var="newline" value="
" /><!--this line can't be indented -->
${fn:replace(myAddress, newline, "<br />")}
回答by bousch
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:
此解决方案比您自己的直接设置 pagecontext 属性的解决方案更优雅。您应该<c:set>
为此使用标签:
<%@ 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.
顺便说一句:${fn:replace(data, "\n", ";")}
不起作用。
回答by bousch
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, "; ")}
回答by Geekygecko
You could create your own JSP function. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags6.html
您可以创建自己的 JSP 函数。 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
创建标签库描述符文件
/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
为函数逻辑创建一个 Java 类。
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.
在您的 JSP 中,您可以通过以下方式访问您的函数。
<%@ taglib prefix="sf" uri="http://www.stackoverflow.com"%>
${sf:clean(item.comments)}
回答by Leonid
回答by Thomas Meyer
This is a valid solution for the JSP EL:
这是 JSP EL 的有效解决方案:
"${fn:split(string1, Character.valueOf(10))}"
回答by Brian Matthews
You should be able to do it with fn:replace.
你应该可以用 fn:replace 做到这一点。
You will need to import the tag library into your JSP with the following declaration:
您需要使用以下声明将标记库导入 JSP:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Then you can use the following expression to replace occurrences of newline in ${data} with a semicolon:
然后,您可以使用以下表达式将 ${data} 中出现的换行符替换为分号:
${fn:replace(data, "\n", ";")}
The documentation is not great on this stuff and I have not had the opportunity to test it.
关于这些东西的文档不是很好,我还没有机会测试它。
回答by Matt Hurne
\n does not represent the newline character in an EL expression.
\n 不代表 EL 表达式中的换行符。
The solution which sets a pageContext
attribute to the newline character and then uses it with JSTL's fn:replace
function does work.
将pageContext
属性设置为换行符然后将其与 JSTL 的fn:replace
函数一起使用的解决方案确实有效。
However, I prefer to use the Jakarta String Tab Library to solve this problem:
不过,我更喜欢使用 Jakarta String Tab Library 来解决这个问题:
<%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %>
...
<str:replace var="result" replace="~n" with=";" newlineToken="~n">
Text containing newlines
</str:replace>
...
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.
你可以使用任何你想要的 newlineToken;“~n”不太可能出现在我正在替换的文本中,所以这对我来说是一个合理的选择。