使用数据库中的换行符存储TextArea数据,并使用相同的格式显示换行符 [英] Storing TextArea data with line breaks in database and displaying in same format with line breaks

查看:170
本文介绍了使用数据库中的换行符存储TextArea数据,并使用相同的格式显示换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

HELLO
WORLD

显示为

HELLO WORLD

HELLO WORLD

我尝试了这里给出的方法如何用< br />替换字符串中的所有换行符?标签?
但它不起作用。我也试过这个如何将用户输入的换行符从TextArea保存到数据库?但我没有使用PHP以及此处给出的另一种语言的解决方案将\ n替换为< br />也不行。请有人知道怎么做吗?

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?

我试过这段代码:

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


推荐答案

replaceAll方法返回一个新的String。您的变量a永远不会被修改。

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

要解决您的问题: a = a.replaceAll(\ n,< br / >);

在这种方法中,阅读 javadoc 节省时间。

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

问候。

这篇关于使用数据库中的换行符存储TextArea数据,并使用相同的格式显示换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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