Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a spring messages.properties, how to ensure line break of error message when using an error code as key?

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.

like image 278
Champ Avatar asked Nov 29 '09 19:11

Champ


4 Answers

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.

like image 145
samitgaur Avatar answered Oct 04 '22 09:10

samitgaur


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 :)

like image 24
skaffman Avatar answered Oct 04 '22 08:10

skaffman


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"); 
like image 41
work.paul Avatar answered Oct 04 '22 08:10

work.paul


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.

like image 40
Bozho Avatar answered Oct 04 '22 09:10

Bozho