保留HTML,JAVA,Spring中的换行符 [英] Preserve line breaks in HTML,JAVA,Spring

查看:318
本文介绍了保留HTML,JAVA,Spring中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用HTML(前端),java(服务器端)
构建的Web应用程序,并且在用换行符发布一些数据时(按下输入后的一个单词)我有一个textarea
没有保留换行符(数据相邻显示,没有换行符)
如何保留换行符,请注意,我在显示时不使用标签(必须)



我使用代码服务器端将新行转换为br

  public String saveLineBreaks (String text){
return text.replaceAll(\\\
,< br />);
}

但它无法正常工作

解决方案

这只是一个疯狂的猜测,因为我不知道你使用的是什么web框架等等,但是:

来自< textarea> 的文本可能会有换行符( \\\
),但HTML将它们解释为空白。所以在Java方面,你需要做这样的事情:

  String forOutput = input.replace(\\\
,< br /> \\\
);

但是,在几乎所有可以想象的Web框架中,都有一些实用方法可以为您手动或自动,所以问题是为你找到合适的人。


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

解决方案

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:

String forOutput = input.replace("\n", "<br />\n");

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.

这篇关于保留HTML,JAVA,Spring中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆