验证错误消息中的 Thymeleaf 单引号 [英] Thymeleaf single quote in validation error message
问题描述
我正在使用验证 API 来验证输入字段是否不包含非法字符:<> ' " 等.我正在使用 @Pattern 注释和自定义消息翻译成这样的东西:以下符号 < > " ' „ 是不允许的.打印此消息时,缺少单引号字符.我有:以下标志<> " „ 不允许.
我试过在 message.properties 中使用 \' 和 \u0027 和 ' 但没有成功(在消息中的第三种情况下有片段 ').消息显示使用:
<p class="error" th:if="${#fields.hasErrors('company.name')}" th:errors="${company.name}">error</p>
Thymeleaf 版本:2.1.3.RELEASE
Spring 版本:3.2.8.RELEASE
Spring webflow 版本:2.4.0.RELEASE
我对 Thymeleaf 了解不多.但请注意,如果您使用 MessageFormat
和 ResourceBundle
检索消息,则可能需要转义单引号(带有两个单引号 (''
))>.
如果您正在使用 Spring 的 ResourceBundleMessageSource
(它在内部使用 ResourceBundle
和 MessageFormat
),您必须做同样的事情.
参见 MessageFormat javadoc:><块引用>
在一个字符串中,一对单引号可以用来引用任何除单引号外的任意字符.例如,模式字符串'{0}'"表示字符串{0}",而不是 FormatElement.单引号本身必须用双引号 '' 表示,贯穿整个细绳.例如,模式字符串'{''}'"被解释为'{(引号的开始和左花括号),''(单个quote) 和 }'(右花括号和引号结尾),而不是 '{' 和'}'(引用左右花括号):代表字符串{'}",不是{}".
前段时间我写了一篇博客文章,提供了关于这个主题的更多细节:Java 资源包的单引号转义
I'm using validation-api to validate if input field doesn't contain illegal characters: < > ' " etc. I'm using @Pattern annotation with custom message translated to something like this: The following signs < > " ' „ are not allowed. When this message is printed the single quote char is missing. I have: The following signs < > " „ are not allowed.
I've tried using \' and \u0027 and ' in the message.properties but with no success (in third case in the message there is fragment '). Messages are displayed using:
<p class="error" th:if="${#fields.hasErrors('company.name')}" th:errors="${company.name}">error</p>
Thymeleaf version: 2.1.3.RELEASE
Spring version: 3.2.8.RELEASE
Spring webflow version: 2.4.0.RELEASE
I don't know much about Thymeleaft. But be aware that you might need to escape single quotes (with two single quotes (''
)) if you retrieve the message using MessageFormat
and ResourceBundle
.
If you are using Spring's ResourceBundleMessageSource
(which uses ResourceBundle
and MessageFormat
internally) you have to do the same thing.
Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String. For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".
Some time ago I wrote a blog post that provides some more details about exactly this topic: Single quote escaping with Java resource bundles
这篇关于验证错误消息中的 Thymeleaf 单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!