0

I am trying to pass new line break with error message from server code to show up in UI. I am displaying these error message with spring's <form:error>

server code:

error= "xyz"+" \n"+"abc"

I want something like this in UI:

xyz

abc (in new line)

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
user0803
  • 1
  • 1

2 Answers2

0

Replacing "\n" with "
" is the quick solution. However, I would caution taking such a simplistic approach as your error messages will look strange if ever used in a non-web environment. For example, if you log the error message to a file, it will show
instead of a line break.

Marshmellow1328
  • 1,205
  • 3
  • 18
  • 27
0

Referring to this answer, if you're using some jsp technology, look for the escape or htmlEscape kinda attributes. And set them to false.

For me I was trying to validate a form with org.springframework.validation.Validator, and was struggling to render a line break in the html code when an email is not valid.

if (!emailId.matches("^.+@.+\\..+$")) {    
    errors.rejectValue("emailId", "error.emailId", "Email ID cannot be blank<br>Should be proper email ID format");
}

After setting htmlEscape="false", it worked!

<td>
    Email ID
</td>
<td>
    <form:input type="email" path="emailId" id="emailId" />
</td>
<td>
    <form:errors path="emailId" />
</td>

I know this answer may be inappropriate for the problem I tried to provide a solution. But, as I couldn't find a exact solution for the issue I was facing, I thought it'd be okay to send it here.