English 中文(简体)
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.
This 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 to display on the jsp page when displaying the error message using the form:errors element.

Instead of , 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 (new line) sequence was escaped. so here is the sample solution, replace the "\n" with " " :

String twoLinesMessage=((String)context.getMessage("error.code", null, locale)).replace("\n","
"); 

Extending the spring tag form:errors and adding conversion from to <br /> is also a non-beatiful solution, but one that will probably work.

Use "<br/>" in place of " " 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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签