简体   繁体   中英

Spring MVC Tomcat Encoding UTF-8

Hello I have a problem with writing in Cyrillic. I use Spring MVC and Tomcat Server. Tomcat web.xml It looks like this :

 <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-mapping>  
<filter-name>encodingFilter</filter-name>  
<url-pattern>/*</url-pattern>  
</filter-mapping> 
<display-name>Welcome to Tomcat</display-name>
<description>
 Welcome to Tomcat
  </description></web-app>

In JSP i add utf-8 in page language and page contentType ,also in form.

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="springForm"
uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Department</title>
</head>
<body>

<p>Въвеждане на нов отдел</p>
<c:if test="${not empty success}">
    <p style="color: green">${success}</p>
</c:if>

<springForm:form action="department" method="post"
    modelAttribute="department" accept-charset="UTF-8">

    <springForm:input type="text" placeholder="Department name"
        path="name" />

    <label style="color: red"> <springForm:errors path="name" /></label>
    <label style="color: red"> ${errorName}</label>

    <br>
    <br>
    <button>Създай</button>
</springForm:form>
</body>
</html>

In DB a set encoding utf8 -default collation ,I changed it to utf 8 general ci , but without effect. Мy characters look like this "нов опиÑ". In the controller I added this code :

department.setName(new String (department.getName().getBytes ("iso-8859-1"), "UTF-8"));

the only change was that it began to look like "??????????????????".

This is my code for DB connection.Do I have to add somewhere UTF-8 and where more accurately.

private static final String DB_PASSWORD = "";
private static final String DB_USER = "root";
private static final String DATABASE = "bms_requests";
private static final String DB_PORT = "3306";
private static final String DB_HOST = "127.0.0.1";
private static final String DB_URL = DB_HOST + ":" + DB_PORT;

private static DBConnection connectionInstance = null;
private static Connection connection = null;

private DBConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://" + DB_URL + "/" + DATABASE, DB_USER, DB_PASSWORD);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public Connection getConnection() {
    return connection;
}

public static DBConnection getInstance() {
    synchronized (DBConnection.class) {
        if (connectionInstance == null) {
            connectionInstance = new DBConnection();
        }
    }

    return connectionInstance;
}

What is wrong and what needs to change.I read a lot and changed a lot, but I could not find a solution to my problem. I'll be glad if one could help me.

Try to add the UTF-8 encoding to the JDBC connection string.

For example:

connection = DriverManager.getConnection("jdbc:mysql://" + DB_URL + "?useUnicode=true&characterEncoding=utf-8" + "/" + DATABASE, DB_USER, DB_PASSWORD);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM