hibernate 验证器 + jsf 2.0:UTF-8 格式的 ValidationMessages.properties

2024-01-31

我在以 UTF-8 格式显示 Hibernate Validator 的自定义 ValidationMessages 时遇到问题。

对于常见的 jsf 消息,我遵循了以下建议:JSF 2.0 应用程序中具有 UTF-8 编码属性文件的 i18n https://stackoverflow.com/questions/3645491/i18n-with-utf-8-encoded-properties-files-in-jsf-2-0-appliaction- 我创建了 Text 类并在 faces-config.xml 中使用它。这是正常工作的。

但这种方法不适用于 ValidationMessages; UTF-8 中不显示特殊字符。

有人可以给我一些建议吗?非常感谢


我已经用同样的方法解决了。 Hibernate 验证器的配置文件位于 META-INF/validation.xml 中

示例验证.xml

<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
    <message-interpolator>com.mycompany.validation.utf8.UTF8ResourceBundleMessageInterpolator</message-interpolator>
</validation-config>

实施UTF 8 ResourceBundle 消息插值器

public class UTF8ResourceBundleMessageInterpolator extends ResourceBundleMessageInterpolator {

    public UTF8ResourceBundleMessageInterpolator() {
        super(new UTF8ResourceBundleLocator(ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES));
    }
}

实施UTF8资源包定位器(PlatformResourceBundleLocator 类的克隆,并进行了小修复)

public class UTF8ResourceBundleLocator implements ResourceBundleLocator {
    private static final Logger logger = LoggerFactory.getLogger(UTF8ResourceBundleLocator.class);

    protected static final ResourceBundle.Control UTF8_CONTROL = new UTF8Control();

    private final String bundleName;

    public UTF8ResourceBundleLocator(String bundleName) {
        this.bundleName = bundleName;
    }


    /**
     * Search current thread classloader for the resource bundle. If not found,
     * search validator (this) classloader.
     *
     * @param locale The locale of the bundle to load.
     * @return the resource bundle or <code>null</code> if none is found.
     */
    @Override
    public ResourceBundle getResourceBundle(Locale locale) {
        ResourceBundle rb = null;
        ClassLoader classLoader = GetClassLoader.fromContext();
        if (classLoader != null) {
            rb = loadBundle(
                    classLoader, locale, bundleName
                            + " not found by thread local classloader"
            );
        }
        if (rb == null) {
            classLoader = GetClassLoader.fromClass(PlatformResourceBundleLocator.class);
            rb = loadBundle(
                    classLoader, locale, bundleName
                            + " not found by validator classloader"
            );
        }

        return rb;
    }

    private ResourceBundle loadBundle(ClassLoader classLoader, Locale locale, String message) {
        ResourceBundle rb = null;
        try {
            rb = ResourceBundle.getBundle(
                    bundleName, locale,
                    classLoader, UTF8_CONTROL
            );
        } catch (MissingResourceException ignored) {
            logger.trace(message);
        }
        return rb;
    }

    private static class GetClassLoader implements PrivilegedAction<ClassLoader> {
        private final Class<?> clazz;

        private static ClassLoader fromContext() {
            final GetClassLoader action = new GetClassLoader(null);
            if (System.getSecurityManager() != null) {
                return AccessController.doPrivileged(action);
            } else {
                return action.run();
            }
        }

        private static ClassLoader fromClass(Class<?> clazz) {
            if (clazz == null) {
                throw new IllegalArgumentException("Class is null");
            }
            final GetClassLoader action = new GetClassLoader(clazz);
            if (System.getSecurityManager() != null) {
                return AccessController.doPrivileged(action);
            } else {
                return action.run();
            }
        }

        private GetClassLoader(Class<?> clazz) {
            this.clazz = clazz;
        }

        @Override
        public ClassLoader run() {
            if (clazz != null) {
                return clazz.getClassLoader();
            } else {
                return Thread.currentThread().getContextClassLoader();
            }
        }
    }
}

Where UTF8控制class 是来自的班级JSF 2.0 应用程序中具有 UTF-8 编码属性文件的 i18n https://stackoverflow.com/questions/3645491/i18n-with-utf-8-encoded-properties-files-in-jsf-2-0-appliaction

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hibernate 验证器 + jsf 2.0:UTF-8 格式的 ValidationMessages.properties 的相关文章

  • 数据表 JSF 中的延迟加载

    在我负责的许多项目中 没有什么比数据表中的延迟分页更好的了 JSF 是否有某种魔力 或者我说得对吗 它确实是一个很大的性能问题 如果你看过一些教程 几乎没有人关心惰性分页 假设您在支持 bean 上有 List 并且数据库中有 2000 行
  • JSF EL 表达式检查属性文件中的空字符串?

    我必须检查我的属性文件对于某些标签是否为空 因为我必须渲染元素 但即使标签为空 我仍然会得到显示键的元素
  • JSF:不同验证器标签的不同验证消息?

    在我的 JSF Primefaces 应用程序中 我正在使用 JSF 验证器标签来验证文本框的输入
  • 使用 f:ajax 渲染多个组件

    错误的代码是
  • JSF-2 应用程序中的服务器端计时器

    在我正在开发的 JSF 2 应用程序中 当用户执行操作时 我需要启动服务器端计时器 这个计时器必须与应用程序本身相关 因此它必须在用户会话关闭时继续存在 为了解决这个问题 我想使用 java util Timer 类在应用程序范围的 bea
  • #1115 - 未知字符集:'utf8mb4'

    我的电脑上运行着一个本地网络服务器 用于本地开发 我现在正处于导出数据库并导入到我的托管 VPS 的阶段 导出然后导入时出现以下错误 1115 未知字符集 utf8mb4 有人能指出我正确的方向吗 该错误明确表明您没有utf8mb4您的阶段
  • JSF 中基于两个组件的组合的验证/转换

    我正在开发一个 JSF Web 应用程序 我需要使用周期性作为数据结构 以下是我使用的 Java 类 public class Periodicity implements Serializable private Integer valu
  • 使用 utf-8 的 Java BufferedWriter 对象

    我有以下代码 我想让输出流使用 utf 8 基本上我有这样的角色 显示为 233 所以看起来像是编码问题 我见过很多使用 的例子 OutputStreamWriter out new OutputStreamWriter new FileO
  • 数据表中每一行的工具提示

    这个问题尖叫着是重复的JSF 2 0 Primefaces 2 x 数据表行的工具提示 https stackoverflow com questions 9980155 jsf 2 0 primefaces 2 x tooltip for
  • R在Windows平台Rstudio上打印data.frames中的UTF-8代码

    当数据框中存在UTF 8字符时 将无法正常显示 例如 以下内容是正确的 gt U6731 1 朱 但是当我将其放入数据框中并打印出来时 它是 gt data frame x U6731 x 1
  • OpenFaces JSF 2 组件库

    有没有人尝试过开放面孔 3 http openfaces org并可以对以下内容进行简短评论 稳定 与其他库的兼容性 PrimeFaces RichFaces 等 使用方便 换肤功能 主题支持等 我正在寻找 JSF 2 组件库 我目前正在使
  • StrRev() 不支持 UTF-8 [重复]

    这个问题在这里已经有答案了 我正在尝试编写一个代码来替换非阿拉伯支持的程序中支持的阿拉伯文本因为我需要在替换后反转文本 但它显示一些垃圾内容而不是想要的结果 这是代码 结果 After Reverse 我需要它是原来的样子 但相反 不是垃圾
  • PHP中如何判断字母是大写还是小写?

    我有 UTF 8 格式的文本 也带有变音符号 并且想检查该文本的第一个字母是大写还是小写 这个怎么做 function starts with upper str chr mb substr str 0 1 UTF 8 return mb
  • 如何解析从java文件中读取的unicode [重复]

    这个问题在这里已经有答案了 我编写了一个包含以下内容的文本文件 u0032 u0142o u017Cy u0142 然后我使用 FileReader 和 BufferedReader 来读取文件 public static void mai
  • 使用:text/plain; 有什么缺点吗?字符集=“UTF-8”

    我的网络服务器提供的内容在 95 的情况下只是简单的 ascii 但在极少数情况下 内容包含一些德语非 ASCII 字符 现在我可以设置content type通过检测内容是否包含任何非 ASCII 字符来响应标头 或者我可以始终设置响应标
  • 为什么 MySQL 将 é 与 e 视为相同?

    我使用 Django Web 应用程序将 Unicode 字符串存储在 MySQL 数据库中 我可以很好地存储 Unicode 数据 但是在查询时 我发现 and e被视为好像它们是同一个角色 In 1 User objects filte
  • 如何在 Flutter 中解码 Gzip Http 响应?

    我是颤振新手 我正在发出网络请求 并且得到了正确的响应 但数据已被压缩 我已经在 Swift 中解压了相同的内容 但是对于 Flutter 我无法做到这一点 有人可以帮忙吗 这是我尝试过的 import dart convert impor
  • .properties 或 JSP 编码有问题

    我有jsp文件
  • JSF 2.x @EJB 依赖注入错误

    因此 正如编程中通常发生的那样 我提出了一个问题 而一个潜在的解决方案又引发了更多问题 和错误 我刚开始使用 servlet JSF 和 EJB 并且遇到了依赖注入错误 这是我原来的问题 JSF h 标签不显示 https stackove
  • 设置开始日期后设置 primefaces 日历结束日期

    我正在使用 primefaces 日历来创建事件 使用 mindate 参数 我禁用了当天之前的日子 即使有结束日期 我也想这样做 禁用开始日期之前的日子 我不知道如何处理这个问题 因为支持 bean 仅在验证整个表单后才获取开始日期 我需

随机推荐