简体   繁体   English

在spring message.properties中,如何在使用错误代码作为键时确保错误消息的换行符?

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

In messages.properties : 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. 现在,当我使用此“ error.code ”为表单字段设置“ errors.rejectValue ”时,在使用以下form:errors显示错误消息时,我无法在jsp页面上显示'\\n'的换行符form:errors元素。

Instead of '\\n' , using <br/> also does not work and gets displayed as is on the page. 而不是'\\n' ,使用<br/>也不起作用,并在页面上显示。

为了显示一个<br>作为换行符,或者错误消息体中的任何其他html标签生效,例如<b> ,只需在标签级别通过添加htmlEscape="false"将html转义为关闭form:errors元素。

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. HTML页面中不同行的布局实际上并不是消息包可以处理的内容,它只是不适合该任务。 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. 然后它成为JSP的工作来呈现消息序列。 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. 我在从数据库加载消息时遇到了同样的问题,这意味着'\\ n'(新行)序列已被转义。 so here is the sample solution, replace the "\\\\n" with "\\n" : 所以这是示例解决方案,将“\\\\ n”替换为“\\ n”:

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

扩展spring标签form:errors并将转换从\\n添加到<br />也是一种非常美丽的解决方案,但可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM