Preserve line breaks in HTML,JAVA,Spring
Solution 1
This is just a wild guess, as I don't know what web framework you are using etc. but:
Text from a <textarea>
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", "<br />\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.
Solution 2
Maybe \n
isn't the line delimiter. Try using System.getProperty("line.separator")
.
Mahmoud Saleh
I am Mahmoud Saleh an Enthusiastic Software Engineer, Computer Science Graduate, Experienced in developing J2EE applications, Currently developing with Spring,JSF,Primefaces,Hibernate,Filenet. Email: [email protected] Linkedin: https://www.linkedin.com/in/mahmoud-saleh-60465545? Upwork: http://www.upwork.com/o/profiles/users/_~012a6a88e04dd2c1ed/
Updated on June 19, 2022Comments
-
Mahmoud Saleh almost 2 years
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", "<br/>"); }
but it doesn't work properly
-
aero over 6 yearsdoesn't work for me, i tried like 10 other versions of this