UTF-8无法在java webapp中使用

时间:2016-05-27 10:20:44

标签: java mysql spring hibernate utf-8

我遵循了教程How to get UTF-8 working in Java webapps?,但它不适用于utf8。我使用Java,Hibernate,Spring,Jsf2,Xhtml和Mysql。我无法在Mysql中插入重音符号,我无法在我的webapp中表示重音符号。我的代码是这样的:

Tomcat Server.xml

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

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

Web.xml中

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>

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


  <filter>
    <filter-name>SetCharacterEncoding</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>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

HTML-meta标签

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <h:head>    
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
     </h:head>

JDBC连接(数据源)

<beans:property name="url"
            value="jdbc:mysql://localhost:3306/Vehiculos?useUnicode=true&amp;characterEncoding=UTF-8" />

JDBC连接(hibernateProperties)

<beans:prop key="hibernate.connection.useUnicode">true</beans:prop>
<beans:prop key="hibernate.connection.characterEncoding">UTF-8</beans:prop>
<beans:prop key="hibernate.connection.charSet">UTF-8</beans:prop> 

MySQL数据库和表

CREATE DATABASE Vehiculos;

CREATE TABLE `Provincias` (
  `id` int(11) unsigned NOT NULL,
  `nombre` varchar(80) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
)ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

MySQL服务器配置

   [client]
   port=3306
   default-character-set=utf8

   [mysql]
   default-character-set=utf8


1 error insert to table
------------------------
mysql> INSERT INTO Provincias VALUES (1, 'Álava');
ERROR 1366 (HY000): Incorrect string value: '\xB5lava' for column 'nombre' at row 1

有人知道在Mysql中插入重音的解决方案吗?并在我的Webapp中表示重音?谢谢。

0 个答案:

没有答案