【问题标题】:JSF2 UTF-8 Encoding issue with saving data into database将数据保存到数据库中的 JSF2 UTF-8 编码问题
【发布时间】:2014-06-30 11:12:55
【问题描述】:

我在 web.xml 文件中添加了以下代码

  <filter>
      <filter-name>forceUTF8CharSet</filter-name>
      <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
      </init-param>
    </filter>

    <filter-mapping>
      <filter-name>forceUTF8CharSet</filter-name>
      <url-pattern>*</url-pattern>
    </filter-mapping>

在数据库中我做了这些更改

CREATE TABLE `group_distribution` (
  `gd_id` int(11) NOT NULL,
  `gd_tweet` varchar(500) CHARACTER SET utf8 NOT NULL,
  `gd_ht` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
  `gt_created_by` int(11) DEFAULT NULL,
  `gt_team_lead` int(11) DEFAULT NULL,
  `gt_send_to` int(11) DEFAULT NULL,
  `gt_added_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `gt_update_dt` timestamp NULL DEFAULT NULL,
  `gt_active_flag` tinyint(1) NOT NULL,
  PRIMARY KEY (`gd_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

当我通过 MySQL 客户端将数据插入 Db 时,它工作正常,但是当我使用 JSF 页面数据库插入相同的数据时,显示这样的数据

??????? ?? ?????? ?? ???????? ?????? ?? ?????? ???? ????? ??? ????? ???????? ??? ?? ?????? ?? ??? ???? ??? Test data

我也在模板文件中添加了这一行

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

JSF 页面能够以印地语字体显示数据,但在转到 Db 时显示相同的数据显示???????

【问题讨论】:

  • 您是否使用正确的charset and collation 创建了数据库架构?
  • 数据库创建于 1 年前,但现在我正在检查 DB 是否支持印地语,并且我可以手动在 DB 中插入印地语字体,但由于应用程序无法正常工作,我已经完成了 SET NAMES 'utf8';
  • 你使用的是哪个应用服务器?
  • 我用的是tomcat7.0.29

标签: java mysql spring hibernate jsf


【解决方案1】:

感谢 Facebook 的 Primefaces Group 成员在我的 application-context.xml 文件中找到了解决方案,我更改了 jdbc url

<property name="jdbcUrl" value="jdbc:mysql://ipaddress:3306/ccc?useUnicode=true&amp;characterEncoding=utf-8" />

以前是这样的

<property name="jdbcUrl" value="jdbc:mysql://ipaddress:3306/ccc " />

【讨论】:

    【解决方案2】:

    尝试将其添加到 web.xml

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

    在 tomcat/config/server.xml 文件中将 URIEncoding="UTF-8" 添加到连接器,如下所示:

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               URIEncoding="UTF-8"/>
    

    【讨论】:

    • 你在tomcat config文件夹的server.xml中添加了UTF8编码吗?
    • 否,但它现在可以工作了,我在 web.xml 和连接 URL 以及 DB 表中添加了
    猜你喜欢
    • 2011-05-06
    • 2011-02-06
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    相关资源
    最近更新 更多