Storing TextArea data with line breaks in database and displaying in same format with line breaks

家住魔仙堡 提交于 2019-12-31 03:49:09

问题


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:

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?

I tried this code:

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

回答1:


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

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

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

Regards.



来源:https://stackoverflow.com/questions/10062358/storing-textarea-data-with-line-breaks-in-database-and-displaying-in-same-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!