如何设置JSF消息编码为UTF-8? [英] How to set JSF message encoding to UTF-8?

查看:177
本文介绍了如何设置JSF消息编码为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用户输入验证的以下代码

I have the following code for some user input validation

<h:form>
    <h:inputText value="#{user.input}">
        <f:validator validatorId="validator.InputValidator" />
    </h:inputText>
    <h:commandButton action="someaction" value="submit">
    </h:commandButton>
    <h:messages layout="table"></h:messages>
</h:form>

它工作正常,但是如果用户输入无效,我需要显示一些UTF-

我该如何做?

It works fine, but I need to show some UTF-8 message if user input is not valid,
how can I do this?

推荐答案

我假设你目前的问题是ISO以外的字符-8859-1范围显示为 mojibake 。这是真的?我不能想到另一个要求这个琐碎问题的原因。是?然后提前读取:

I assume that your current problem is that characters outside the ISO-8859-1 range are been shown as mojibake. Is this true? I couldn't think of another reason for asking this trivial question. Yes? Then read ahead:

首先,如果您仍然使用旧的JSP而不是后继的Facelets,那么您需要将页面编码设置为UTF-8。将此放在每个JSP之上:

First, if you're still using old JSP instead of its successor Facelets, then you need to set the page encoding to UTF-8. Put this in top of every JSP:

<%@page pageEncoding="UTF-8" %>

或在 web.xml中全局配置

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

如果你使用Facelets,你不需要做任何事情。它默认使用UTF-8作为响应编码。

If you're using Facelets, you don't need to do anything. It uses by default already UTF-8 as response encoding.

其次,如果您从 .properties获取消息文件,那么您需要了解这些文件默认是使用ISO-8859-1读取的。另请参见 java.util.Properties javadoc

Second, if you're obtaining the messages from .properties files, then you need to understand that those files are by default been read using ISO-8859-1. See also java.util.Properties javadoc:


..输入/输出流以ISO 8859-1字符编码。在此编码中无法直接表示的字符可以使用Unicode转义写入;在转义序列中只允许单个u字符。 native2ascii 工具可用于将属性文件转换为其他字符编码或从其他字符编码转换。

.. the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

因此,您需要通过 Unicode转义序列来写下ISO-8859-1范围之外的字符。 / a>。例如。而不是

So, you need to write down the the characters outside the ISO-8859-1 range by Unicode escape sequences. E.g. instead of

some.dutch.text = Één van de wijken van Curaçao heet Saliña.

您需要写

some.dutch.text = \u00c9\u00e9n van de wijken van Cura\u00e7ao heet Sali\u00f1a.

这可以通过使用 native2ascii 工具。

This can be automated by using native2ascii tool.

作为完全不同的选择,您可以提供JSF自定义 ResourceBundle 实现自定义 Control ,它使用UTF-8读取文件。详情请参阅此答案此博客。这只有当你提供自己的验证消息,例如 requiredMessage ,而不是覆盖JSF默认验证消息。除此之外(即你需要< message-bundle> 文件),那么你真的需要使用 native2ascii 工具。

As a completely different alternative, you can supply JSF a custom ResourceBundle implementation with a custom Control which reads the files using UTF-8. This is in more detail expanded in this answer and this blog. This works only whenever you're providing validation messages by your own as for example requiredMessage instead of overriding JSF default validation messages. Other than that (i.e. you need it for <message-bundle> files), then you really need to use the native2ascii tool.

  • Unicode - How to get the characters right?

这篇关于如何设置JSF消息编码为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆