Save line breaks in HTML, JAVA, Spring

I have a web application built using HTML (front-end), java (server side) and I have textarea when you send some data with line breaks (by pressing enter after the word) line breaks are not reserved (data appears next to each other without line breaks) how to keep line breaks ?, note that I do not use the tag when displaying (should)

I use the server side of the code to convert newlines to br

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

but it does not work properly

+4
source share
2 answers

This is just a wild hunch, as I don’t know what kind of web infrastructure you are using, etc., but:

Text from <textarea> is likely to have line breaks ( \n ), but HTML interprets 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 all imaginary web environments, there is a utility method that does this for you manually or automatically, so the question is to find the right one for you.

+5
source

Perhaps \n not a line separator. Try using System.getProperty("line.separator") .

+2
source

All Articles