【问题标题】:JSF Encode UTF - 8?JSF编码UTF-8?
【发布时间】:2011-04-27 03:12:39
【问题描述】:

现在我正在和我的朋友一起工作,他是越南人,他想使用越南语创建一个网站,但我们遇到了编码 UTF-8 的问题。 我写了一个Filter类如下:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharsetFilter implements Filter
{
    private String encoding;   

    public void init(FilterConfig config) throws ServletException
    {
        encoding = config.getInitParameter("requestEncoding");
        if( encoding==null ) encoding="UTF-8";
     }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain  next)   throws IOException, ServletException
    {
        // Respect the client-specified character encoding
        // (see HTTP specification section 3.4.1)
        if(null == request.getCharacterEncoding())
            request.setCharacterEncoding(encoding);
        /** * Set the default response content type and encoding */
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        next.doFilter(request, response);
        }

    @Override
    public void destroy() {}
}

web.xml 中的配置:

<filter>
        <filter-name>CharsetFilter</filter-name>
        <filter-class>com.mypackage.CharsetFilter</filter-class>
        <init-param>
                <param-name>requestEncoding</param-name>
                <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

在服务器资源 - sun-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="vietweb" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="vietweb"/>
        <property name="User" value="root"/>
        <property name="Password" value="12345"/>
         <property name="useUnicode" value="true"/>
        <property name="characterEncoding" value="UTF8"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/vietweb"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  </jdbc-connection-pool>
  <jdbc-resource enabled="true" jndi-name="jdbc/vietweb" object-type="user" pool-name="mysql_vietweb_rootPool"/>
</resources>

在 JSF 页面中:

<meta http-equiv="content-type" content="text/html; charset=utf-8"/>

但它没有被编码为越南语。 我们怎样才能让它编码成越南语?

将数据保存到数据库的问题(我使用的数据库是 MySQL) 我想要/期待这个输出:

chuyển phát nhanh để chuyển tới những cuốn sách

但我看到了这个意外的输出:

chuy?n phát nhanh ?? chuy?n t?i nh?ng ??u sách 

【问题讨论】:

  • 数据是否来自数据库?
  • 到底哪里出错了?在 JSF 页面中显示数据库数据或将 JSF 表单参数保存在数据库中?意外的结果究竟如何?更新您的问题以包含预期结果和意外结果的副本(只需一个单词或简单的句子就足够了)。
  • 嗨 Odelya 和 BalusC 我正在编辑我的问题,它来自将数据保存到数据库中并显示?字符

标签: mysql jsf unicode character-encoding


【解决方案1】:

我已经通过这句话解决了我的问题:

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

【讨论】:

  • 这不是 OP 的具体问题的原因。 OP 已经按照 Glassfish 数据源配置中的&lt;property name="useUnicode" value="true"/&gt;&lt;property name="characterEncoding" value="UTF8"/&gt; 使用此配置。
【解决方案2】:

这些症状表明 MySQL 数据库和/或表创建为 ISO-8859-x 而不是 UTF-8。任何超出 ISO-8859-x 范围的字符都将存储为?。按如下方式更新数据库和/或表:

ALTER DATABASE dbname DEFAULT CHARACTER SET utf8;
USE dbname;
ALTER TABLE tblname CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

webapp 和 DB 连接设置非常好。否则你会看到mojibake 而不是问号。


也就是说,CharsetFilter 本质上是不必要的。默认情况下,Facelets 上的 JSF 已经使用 UTF-8。当您仍在使用旧版 JSP 而不是 Facelets 时,您需要做的就是将以下行添加到 JSP 顶部:

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

并且只在过滤器中执行request.setCharacterEncoding()(对response 不执行任何操作)。

另见:

【讨论】:

  • 非常感谢 Balus,我有一个小问题是如何在 h:outputText 中显示 HTML 标记,因为我保存到 DB 中的是纯文本附加 HTML,我想在 JSF 页面上显示它。谢谢!
  • 添加escape="false"。牢记 XSS 风险!另请参阅我昨天发布的this answer
  • 谢谢,我也找到了:D
  • 对不起,Balus 的愚蠢问题,为什么它会返回?在服务器上重新部署应用程序后?
  • 您必须修复格式错误的数据库内容。
猜你喜欢
  • 2012-06-15
  • 1970-01-01
  • 2011-10-30
  • 2012-11-07
  • 2011-11-06
  • 2015-03-04
  • 2016-07-05
  • 1970-01-01
  • 2015-11-15
相关资源
最近更新 更多