struts2 中文亂碼的解決辦法分享_java

來源:互聯網
上載者:User

適合情況 -> 從jsp傳入到action時的亂碼情況,這裡以GBK為例

1.建立一個用於轉換編碼的filter
檔案位置舉例:src.util.SetCharacterEncodingFilter.java

複製代碼 代碼如下:

package util;

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;
import javax.servlet.UnavailableException;

/**
 * Example filter that sets the character encoding to be used in parsing the
 * incoming request
 */
public class SetCharacterEncodingFilter implements Filter {

    /**
     * Take this filter out of service.
     */
    public void destroy() {
    }
    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     */
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain)throws IOException, ServletException {

    request.setCharacterEncoding("gbk");

    // 傳遞控制到下一個過濾器
    chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

2.修改web.xml,在struts的FilterDispatcher映射之前添加2個filter

複製代碼 代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
    <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>util.SetCharacterEncodingFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>Set Character Encoding</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

   

   
 <filter>
  <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

3.你的JSP頭應該有
<%@ page language="java" pageEncoding="GBK"%>

4.在struts.xml中修改預設的編碼設定

複製代碼 代碼如下:

<struts>
 <constant name="struts.i18n.encoding" value="gbk"></constant>

...
...
...

</struts>

基本上就這樣可以解決大多傳入的字元亂碼問題

PS:如果是資料庫提取字元亂碼,比如mysql,確認你的資料庫內字元是gbk,並且連接字串指定了字元編碼
<property name="url" value="jdbc:mysql://localhost/database?useUnicode=true&characterEncoding=gbk"></property>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.