将 JSF 输入提交值的请求字符编码设置为 UTF-8 [英] Set request character encoding of JSF input submitted values to UTF-8

查看:26
本文介绍了将 JSF 输入提交值的请求字符编码设置为 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的问题 在 GlassFish 中将 JSF 输入提交值的请求字符编码设置为 UTF-8,提交的值作为 Mojibake 到达.但是,答案是针对 GlassFish 而我使用的是 JBoss AS 7.

I have the same problem as Set request character encoding of JSF input submitted values to UTF-8 in GlassFish, the submitted values arrive as Mojibake. However, the answer is targeted at GlassFish and I'm using JBoss AS 7.

我已经指定了使用 UTF-8 的 JDBC 连接 URL:

I've already specified the JDBC connection URL to use UTF-8:

jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8

在我的 JSF 页面顶部:

And in top of my JSF page:

<?xml version='1.0' encoding='UTF-8' ?>

如何在 JBoss AS 7 中解决同样的问题?或者更好的是,以更通用的方式使其适用于所有服务器?

How can I solve the same problem in JBoss AS 7? Or better, in a more generic way so that it works in all servers?

推荐答案

您链接的问题已经排除了 DB 编码的原因,因为在保存到 DB 之前打印/重新显示提交的值时已经出现问题.因此,问题在于 HTTP 请求编码.

The question which you linked to has already excluded the DB encoding from being the cause because the problem already occurs during printing/redisplaying the submitted value before saving in DB. Thus, the problem is in HTTP request encoding.

带有指定字符集的 JDBC 连接 URL,

Your JDBC connection URL with the charset specified,

jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8

只告诉 MySQL JDBC 驱动程序在将 SQL 查询中的值发送到数据库之前使用 UTF-8 对其进行解码.这不仅完全超出了 JSF 的范围,而且这也不是问题的原因,前提是您绝对肯定您遇到了与链接问题相同的问题.

only tells the MySQL JDBC driver to use UTF-8 to decode values in SQL queries before sending it to DB. This is not only completely beyond JSF's scope, but this is also not the cause of your problem, provided that you're absolutely positive that you've the same problem as in the linked question.

带有指定字符集的 XML 序言,

Your XML prolog with the charset specified,

<?xml version='1.0' encoding='UTF-8' ?>

只告诉 XML 解析器在围绕它构建 XML 树之前使用 UTF-8 来解码 XML 源.实际使用的 XML 解析器是在 JSF 视图构建期间由 Facelets 在内部使用的 SAX.这部分与 HTTP 请求/响应编码完全无关,因此不太可能导致您的问题.

only tells the XML parser to use UTF-8 to decode the XML source before building the XML tree around it. The XML parser actually being used is SAX as internally used by Facelets during JSF view build time. This part has completely nothing to do with HTTP request/response encoding and is thus very unlikely the cause of your problem.

它们都没有设置HTTP请求编码,而您需要设置HTTP请求编码.您链接到的问题已经显示了如何为 Glassfish 服务器执行此操作.就您而言,您使用的是 JBoss AS 服务器.Glassfish 特定的设置然后不适用,JBoss 不支持任何类似的东西.您需要引入自定义 servlet 过滤器来完成这项工作.例如

None of them sets the HTTP request encoding, while you need to set the HTTP request encoding. The question which you linked to already shows how to do that for the Glassfish server. In your case, you're however using JBoss AS server. The Glassfish-specific setting is then inapplicable and JBoss doesn't support anything like that. You'd need to bring in a custom servlet filter to do the job. E.g.

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    // ...
}

这篇关于将 JSF 输入提交值的请求字符编码设置为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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