在 HTML、JAVA、Spring 中保留换行符
我有一个用 HTML(前端)、java(服务器端)构建的 Web 应用程序 我在发布一些带有换行符的数据时有一个文本区域(在单词后按 Enter 键) 不保留换行符(数据彼此相邻显示,没有换行符) 如何保留换行符?请注意,我在显示时没有使用标签(必须)
我正在使用代码服务器端将新行转换为 br
public String saveLineBreaks(String text) {
return text.replaceAll("\n", "<br/>");
}
但它无法正常工作
i have a web application built with HTML(front-end),java(server-side)
and i have a textarea when posting some data with line breaks (pressing enter after a word)
the line breaks are not reserved (the data appears next to each other with no line breaks)
how to preserve the line breaks ?, note that i am not using the tag when displaying (have to)
i am using the code server side to convert new lines into br
public String saveLineBreaks(String text) {
return text.replaceAll("\n", "<br/>");
}
but it doesn't work properly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是一个疯狂的猜测,因为我不知道您正在使用什么网络框架等,但是:
来自
然而,在几乎所有可以想象到的 Web 框架中,都有一些实用方法可以手动或自动为您执行此操作,因此问题是找到适合您的方法。
This is just a wild guess, as I don't know what web framework you are using etc. but:
Text from a
<textarea>
will probably have line breaks (\n
), but HTML will interpret them as whitespace. So on the java side, you need to do something like this:However, in almost every imaginable web framework, there is some utility method that does this for you manually or automatically, so the question is to find the right one for you.
也许
\n
不是行分隔符。尝试使用 System.getProperty("line.separator") 。Maybe
\n
isn't the line delimiter. Try usingSystem.getProperty("line.separator")
.