java html换行符号,保留HTML,JAVA,Spring中的换行符

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", "
");

}

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 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", "
\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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值