用数据库中的换行符存储文本区域数据,用换行符显示相同的格式

时间:2021-03-15 18:18:14

I have a JSP page with a textarea HTML component. When a user presses enter and goes to next line while typing in textarea,and when he clicks save button, it saves the data from the text area to the database. But when I load the content of the database, the line breaks are gone. Means:

我有一个带有textarea HTML组件的JSP页面。当用户在输入文本区域时按enter并转到下一行,当用户单击save按钮时,它将数据从文本区域保存到数据库。但是当我加载数据库的内容时,换行符就消失了。意思是:

HELLO
WORLD

你好,世界

is displaying like

显示如

HELLO WORLD

你好,世界

I tried the method given here How do I replace all line breaks in a string with <br /> tags? but its not working. I also tried this one How to save user-entered line breaks from a TextArea to a database? but I am not using PHP and also the solution given here for another languages to replace "\n" with "<br />" also not working. Please anyone have any idea how to do that?

我尝试了这里给出的方法,如何用
标记替换字符串中的所有换行符?但它不工作。我也尝试过这个如何将用户输入的换行符从文本区域保存到数据库?但是,我没有使用PHP,这里给出的另一种语言用“
”替换“\n”的解决方案也不起作用。有人知道怎么做吗?

I tried this code:

我试着这段代码:

String a=req.getParameter("topicdes").toString();
a.replaceAll("\n","<br />");

1 个解决方案

#1


3  

The replaceAll method return a new String. Your variable a is never modified.

replaceAll方法返回一个新字符串。变量a永远不会被修改。

To solve your problem : a = a.replaceAll("\n","<br />");

要解决你的问题:a = a。replaceAll(“\ n”、“< br / >”);

In this kind of method, reading the javadoc is time saver.

在这种方法中,读取javadoc可以节省时间。

Regards.

的问候。

#1


3  

The replaceAll method return a new String. Your variable a is never modified.

replaceAll方法返回一个新字符串。变量a永远不会被修改。

To solve your problem : a = a.replaceAll("\n","<br />");

要解决你的问题:a = a。replaceAll(“\ n”、“< br / >”);

In this kind of method, reading the javadoc is time saver.

在这种方法中,读取javadoc可以节省时间。

Regards.

的问候。