开发者

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 开发者_JS百科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.


Use "<br/>" in place of "\n" in your code.

For example:

return i + " x " + j + " = "+ (Integer.valueOf(i * j) + "<br/>"));

This returns the multiplication table in the web with line breaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜