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

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

问题描述

我遇到了与字符编码JSF输入相同的问题,提交的值以< a href =http://en.wikipedia.org/wiki/Mojibake =nofollow> Mojibake 。但是,答案是针对GlassFish的,我使用JBoss AS 7。

I have the same problem as Character encoding JSF input, the submitted values arrive as Mojibake. However, the answer is targeted at GlassFish and I'm using JBoss AS 7.

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

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中。

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查询中的值发送到DB之前使用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天全站免登陆