In messages.properties
:
error.code=This is error message.\nThis is next line of error message.
Now, when I set "errors.rejectValue
" with this "error.code
" for a form field, I cannot get the line break of '\n'
to display on the jsp page when displaying the error message using the form:errors
element.
Instead of '\n'
, using <br/>
also does not work and gets displayed as is on the page.
In order to display a <br>
as a line break, or for any other html tag in the error message body to take effect e.g. a <b>
, simply turn html escaping off at tag level by adding htmlEscape="false"
to your form:errors
element.
The layout of distinct lines in an HTML page is not really something that a message bundle can deal with, it's just not suitable for the task. If you need multiple lines to be displayed, then realistically you're going to need multiple messages, with multiple entries in the properties file.
Perhaps you could cook up something which iterates over a sequence of properties, with something like this in your properties file:
error.code.1=This is error message.
error.code.2=This is next line of error message.
It then becomes the job of the JSP to render the sequence of messages. Not very elegant, though, but I can't think of a better solution right now :)
i had the same problem while loading the messages from the database, meaning that the '\n' (new line) sequence was escaped. so here is the sample solution, replace the "\\n" with "\n" :
String twoLinesMessage=((String)context.getMessage("error.code", null, locale)).replace("\\n","\n");
Extending the spring tag form:errors
and adding conversion from \n
to <br />
is also a non-beatiful solution, but one that will probably work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With