Thymeleaf: replace newline characters with <br>

thymeleaf newline th-text
thymeleaf tutorial
thymeleaf strings
thymeleaf th:with
thymeleaf substring
th:remove
thymeleaf table
thymeleaf readonly

I have a field (<textarea name="desc" />) that may contain newlines and I want to replace them with their HTML counterpart: <br />. How can I do this? I'm using Thymeleaf 2.1.4.RELEASE.

As stated by dominik, \n for newline does not work. However, you can use &#10;.

${#strings.replace(desc,'&#10;','&lt;br&gt;')}

Or with escaping to prevent code injection:

${#strings.replace(#strings.escapeXml(desc),'&#10;','&lt;br&gt;')}

General Usage - replacete new line into <br /> - Thymeleaf, Hello, I want to let users in comment field (textarea) enter new line. You'll have to process the user input first, replacing \n with <br/> where  exception is being thrown as the Thymeleaf underlying XML parser does not allow to put < and > inside expressions; The solution I found for the first problem is to set newline character in a controller and pass it to view. To address the second problem you need to use &lt; instead of < and &gt; instead of >.

Thymeleaf: replace newline characters with <br>, Thymeleaf: replace newline characters with <br>. Question. I have a field ( <​textarea name="desc" /> ) that may contain newlines and I want to replace them with  When render the above HTML on Web Browser(Chrome, Firefox, Safari, IE ), first new line character is removed from User Interface. On an Web Browser,"<CRLF>input value" is render same with "input value" (without <CRLF>).

First new line character is removed when use <textarea th:field , When first character of input value is new line character, is output the HTML as However child text( <span th:text="*{text}" th:remove="tag"></span> ) has kazuki43zoo added a commit to kazuki43zoo/thymeleaf-spring that  Out-of-the-box, Thymeleaf allows you to process six kinds of templates, each of which is called a Template Mode: HTML XML TEXT JAVASCRIPT CSS RAW There are two markup template modes ( HTML and XML ), three textual template modes ( TEXT, JAVASCRIPT and CSS) and a no-op template mode ( RAW ).

java - Thymeleaf: replace newline characters with <br>, i have field ( <textarea name="desc" /> ) may contain newlines , want replace them html counterpart: <br /> . how can this? i'm using thymeleaf 2.1.4.release. Well, there a number of ways to specify a new line in different areas in python. Python New Line Character. For most of the purposes, the newline character can be used to specify a new line. You can put this character within Python Strings. An example of using character in a string to display output to the console screen is given below.

Thymeleaf에서 newline characters를 br tag로 교체, 일반적으로는 다음과 같이 new line 문자를 br tag로 치환합니다. String text = "this is test\nsecond line"; String textHtml = text.replace("\n", "<br/>  Thymeleaf is a Java template engine for processing and creating HTML, XML, JavaScript, CSS and plain text. For an intro to Thymeleaf and Spring, have a look at this write-up. Besides these basic functions, Thymeleaf offers us a set of utility objects that will help us perform common tasks in our application.

Spring and Thymeleaf 3: Expressions, Thymeleaf is a Java template engine for processing and creating HTML, XML, JavaScript, CSS and plain text. For an intro to Thymeleaf and  Since \ is used in the search portion, why not use in the replace ? the former seems to imply the latter exists. ie. why not this $ echo '1 2'|sed 's/\ / /g' – user306023 Sep 6 '18 at 19:24 In this case, is a literal, implying that it appears as the two characters: abackslash followed by n , and not as a single newline character.

Comments
  • Am I missing something if I understand this as replacing text characters in some model attribute (not rendered HTML)? If so, then why don't you just do it in the controller using plain Java?
  • See also stackoverflow.com/q/37765352/883073
  • @HalleKnast: This kind of job is view-specific. What would happen if you added another view implementation that renders *.doc files? You would end up with model attribute exposing string with "<br />"s that are invalid in .doc format - that's why it's not a job for controllers.
  • Just to help people. &#10; is the ASCII of the LineFeed character. It worked like a charm :)
  • really? this does not work for me and looks like so complicated to do something trivial