IntelliJ Java IDE
The moose likes JSP and the fly likes New Line character in JSTL c:set Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "New Line character in JSTL c:set" Watch "New Line character in JSTL c:set" New topic
Author

New Line character in JSTL c:set

Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
Hi,
I want to append a new line character('\n') at the end of a string and then display it so that they come one below the other.
I tried something like this -
<c:set var="tempStr" scope="page" value="${someStr}\n" />
This is not working. Can somebody tell me the correct way of going about it.
Thanks in advance,
Sam.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You could try:
<c:set var="tempStr" scope="page" value="${someStr + '\n'}" />
EL will concatenate the String with the \n character.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Also, I hope you realize, HTML rendering ignores white space. If you output a new line character into html, the source gets the new line but, when rendered all white space gets mushed down to a single space.
If you want this rendered on a new line in html ouput, you need to put a <br> after it.
chang Anne
Greenhorn

Joined: Jul 02, 2003
Posts: 29
Hello,
chang Anne
Greenhorn

Joined: Jul 02, 2003
Posts: 29
Hello,
I would like to try new line in jstl run_time tag, but I get the following
errorsrg.apache.jasper.JasperException: /SimpleBean.jsp(11,0) "${header['User-Agent']+ '\n'}" contains invalid expression(s): javax.servlet.jsp.el.ELException: Encountered "'\n", expected one of [, , , "true", "false", "null", "(", "-", "not", "!", "empty", ]
And my code runs in jsp2.0
Any ideas?
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1825
Hi Chang,
Why dont you look at the suggestion by Carl and at the br tag in the end like the one given below
chang Anne
Greenhorn

Joined: Jul 02, 2003
Posts: 29
Hello,
I just want to try add a new ('\n') in JSTL.
Thank your suggestion.
Jarek Lipski
Greenhorn

Joined: Sep 06, 2008
Posts: 1
In JSP 2.0: open string literal using " or ', press enter and close string literal :-)

For example:

${fn:replace(text, "
", "<br/>")}

Regards,
Jarek
[ September 06, 2008: Message edited by: Jarek Lipski ]
Steve Perkins
Greenhorn

Joined: Jan 24, 2012
Posts: 1
There may be other ways to accomplish this, but here's a method that worked for me. Near the top of the page, I declared a variable holding the newline character:



I was then able to use this variable in an expression down in the "<pre>" block:



Hope this helps someone who stumbles across this page by using the same Google terms as I did.
 
IntelliJ Java IDE
 
subject: New Line character in JSTL c:set
 
Threads others viewed
Output of array only on one line - any ideas why?
NewLine Problem in Regular Expressions
create and execute a batch file through a java program
New Line Character not recognized
Problem with new line character !!!
IntelliJ Java IDE