首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Struts + Hibernate 中文亂碼終極解決方案:全鏈路編碼過濾器實戰

     編輯:Java 相關知識

問題根源

Struts + Hibernate 應用中,HTTP 請求參數編碼、JSP 頁面編碼、數據庫連接編碼、JVM 文件編碼不一致,導致中文存儲讀取亂碼。核心缺口:Servlet 規範未強制要求容器按指定編碼解碼請求參數。

解決方案:字符編碼過濾器

基於 Servlet Filter 攔截所有請求,在參數讀取前強制設置編碼。

1. web.xml 註冊過濾器 (務必放在首位)

<filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>com.company.demo.util.SetEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>  <!-- 或 UTF-8 -->
  </init-param>
</filter>
<filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

2. 過濾器實現 (SetEncodingFilter.java)

package com.company.demo.util;
import java.io.IOException;
import javax.servlet.*;

public class SetEncodingFilter implements Filter {
    protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true; // 默認忽略客戶端指定編碼

    public void destroy() {
        encoding = null; filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 若忽略模式 或 請求未設置編碼,則強制設置
        if (ignore || request.getCharacterEncoding() == null) {
            request.setCharacterEncoding(selectEncoding(request));
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if (value == null) this.ignore = true;
        else this.ignore = "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
    }

    protected String selectEncoding(ServletRequest request) {
        return this.encoding; // 可擴展:根據請求動態決定編碼
    }
    // getter/setter 省略
}

全鏈路編碼一致性檢查清單

進階建議

  • 統一 UTF-8:新項目強烈建議全棧 UTF-8,避免 GB2312/UTF-8 混用轉換損耗
  • 過濾器順序:編碼過濾器必須在 ActionContextCleanUp、OpenSessionInViewFilter 等之前
  • 響應編碼:response.setContentType("text/html;charset=UTF-8") 或 JSP 頁面指令雙重保障
  • GET 請求:Tomcat 8+ 需在 server.xml <Connector URIEncoding="UTF-8"/> 解決 GET 參數亂碼

總結

字符編碼過濾器是解決 Struts/Hibernate 中文亂碼的標準化、可復用方案。配合全鏈路編碼一致性檢查,可徹底消除「存進去是亂碼、讀出來是問號」的持久化層噩夢。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
環節配置要點
JSP 頁面<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
web.xml過濾器 encoding=UTF-8;<jsp-config><jsp-property-group><page-encoding>UTF-8</page-encoding></jsp-property-group></jsp-config>
Strutsstruts-config.xml controller contentType="text/html;charset=UTF-8"
Hibernate數據庫連接 URL 追加 ?useUnicode=true&characterEncoding=UTF-8;MySQL 表/列 CHARSET=utf8
數據庫CREATE DATABASE ... DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
JVM啟動參數 -Dfile.encoding=UTF-8 (慎用,影響日志等)