`
joechoate
  • 浏览: 4003 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

struts、spring、hibernate中文乱码问题解决

阅读更多
struts、spring、hibernate中文乱码问题解决
2008年01月08日 星期二 18:44
      近日,使用struts 1.1,发现讨厌的中文乱码问题,在form的传送过程和入库时候出现。就我在网络上找的方法罗列如下:
(Tomcat 5.0.28+struts 1.1+hibernate 2.1+sqlserver2k)
1.直接转编码
public static String isoToGB(String src)
{   
    String strRet=null;  
    try{   
            strRet = new String(src.getBytes("ISO_8859_1"),"GB2312"); 
    }catch(Exception e)    {        
    } 
  return strRet;
}

通过一个函数转编码,我没有成功,不知为何!

2.过滤filter设置法

package yourbean;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class servfilter extends HttpServlet implements Filter {
private FilterConfig filterConfig; //Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) {    this.filterConfig = filterConfig; } //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {  
     try {      request.setCharacterEncoding("GB2312");     
((HttpServletResponse)response).setHeader("Cache-control","no-cache");   
  response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setHeader("Expires","0");    
  ((HttpServletResponse)response).setHeader("Pragram","no-cache");     
filterChain.doFilter(request, response);    }   
catch(ServletException sx) {      filterConfig.getServletContext().log(sx.getMessage());    }   
catch(IOException iox) {      filterConfig.getServletContext().log(iox.getMessage());    } } //Clean up resources
public void destroy() { }
}下面是一个web.xml文件你用jbuilder写上面的bean的时候会生成一个<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app> <display-name>Welcome to Tomcat</display-name> <description>     Welcome to Tomcat </description> <filter>    <filter-name>servfilter</filter-name>    <filter-class>yourbean.servfilter</filter-class> </filter> <filter-mapping>    <filter-name>servfilter</filter-name>    <url-pattern>/*</url-pattern> </filter-mapping></web-app>把上面的servfilter编译放在你的web-inf/classes/yourbean/下web.xml放在web-inf/下和classes在一个目录下在每个jsp页面上加上<%@page contentType="text/html;charset=GBK"%>

也不是很方便,而且在tomcat也没有成功,继续郁闷!

3.我现在使用方法,推荐!!

写一个myActionServlet来并覆盖ActionServlet中的process()方法。

protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {    /**@todo Override this org.apache.struts.action.ActionServlet method*/    request.setCharacterEncoding("GB2312");//就加着一行一切都解决了    super.process(request, response); }

当然别忘了改一下web.xml里面的配置 <servlet>    <servlet-name>action</servlet-name>    <servlet-class>strutsdemo.myActionServlet</servlet-class>    <init-param>      <param-name>debug</param-name>      <param-value>2</param-value>    </init-param>    <init-param>      <param-name>config</param-name>      <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <load-on-startup>2</load-on-startup> </servlet>

改一下servlet-class标签中的内容就可以!

真的可以,一劳用yi!

具体编码的理论就不说了,google上已经够多了。

另外,如果不用struts的话,hibernate也可能碰到中文乱码问题,只要在hibernate.cfg.xml配置中如下:

<property name="hibernate.connection.url">    jdbc:microsoft:sqlserver://Localhost:1433;SelectMethod=cursor;characterEncoding=GBK;DatabaseName=myDatabase.   </property>

characterEncoding=GBK!就可以了。



********************************************************************************************************************

Java/J2EE中文问题终极解决之道
Java中文问题一直困扰着很多初学者,如果了解了Java系统的中文问题原理,我们就可以对中文问题能够采取根本的解决之道。
  最古老的解决方案是使用String的字节码转换,这种方案问题是不方便,我们需要破坏对象封装性,进行字节码转换。

  还有一种方式是对J2EE容器进行编码设置,如果J2EE应用系统脱离该容器,则会发生乱码,而且指定容器配置不符合J2EE应用和容器分离的原则。

  在Java内部运算中,涉及到的所有字符串都会被转化为UTF-8编码来进行运算。那么,在被Java转化之前,字符串是什么样的字符集? Java总是根据操作系统的默认编码字符集来决定字符串的初始编码,而且Java系统的输入和输出的都是采取操作系统的默认编码。

  因此,如果能统一Java系统的输入、输出和操作系统3者的编码字符集合,将能够使Java系统正确处理和显示汉字。这是处理Java系统汉字的一个原则,但是在实际项目中,能够正确抓住和控制住Java系统的输入和输出部分是比较难的。J2EE中,由于涉及到外部浏览器和数据库等,所以中文问题乱码显得非常突出。

  J2EE应用程序是运行在J2EE容器中。在这个系统中,输入途径有很多种:一种是通过页面表单打包成请求(request)发往服务器的;第二种是通过数据库读入;还有第3种输入比较复杂,JSP在第一次运行时总是被编译成Servlet,JSP中常常包含中文字符,那么编译使用javac时,Java将根据默认的操作系统编码作为初始编码。除非特别指定,如在Jbuilder/eclipse中可以指定默认的字符集。

  输出途径也有几种:第一种是JSP页面的输出。由于JSP页面已经被编译成Servlet,那么在输出时,也将根据操作系统的默认编码来选择输出编码,除非指定输出编码方式;还有输出途径是数据库,将字符串输出到数据库。

  由此看来,一个J2EE系统的输入输出是非常复杂,而且是动态变化的,而Java是跨平台运行的,在实际编译和运行中,都可能涉及到不同的操作系统,如果任由Java自由根据操作系统来决定输入输出的编码字符集,这将不可控制地出现乱码。

  正是由于Java的跨平台特性,使得字符集问题必须由具体系统来统一解决,所以在一个Java应用系统中,解决中文乱码的根本办法是明确指定整个应用系统统一字符集。

  指定统一字符集时,到底是指定ISO8859_1 、GBK还是UTF-8呢?

  (1)如统一指定为ISO8859_1,因为目前大多数软件都是西方人编制的,他们默认的字符集就是ISO8859_1,包括操作系统Linux和数据库MySQL等。这样,如果指定Jive统一编码为ISO8859_1,那么就有下面3个环节必须把握:

  开发和编译代码时指定字符集为ISO8859_1。

  运行操作系统的默认编码必须是ISO8859_1,如Linux。

  在JSP头部声明:<%@ page contentType="text/html;charset=ISO8859_1" %>。

  (2)如果统一指定为GBK中文字符集,上述3个环节同样需要做到,不同的是只能运行在默认编码为GBK的操作系统,如中文Windows。

  统一编码为ISO8859_1和GBK虽然带来编制代码的方便,但是各自只能在相应的操作系统上运行。但是也破坏了Java跨平台运行的优越性,只在一定范围内行得通。例如,为了使得GBK编码在linux上运行,设置Linux编码为GBK。

  那么有没有一种除了应用系统以外不需要进行任何附加设置的中文编码根本解决方案呢?

  将Java/J2EE系统的统一编码定义为UTF-8。UTF-8编码是一种兼容所有语言的编码方式,惟一比较麻烦的就是要找到应用系统的所有出入口,然后使用UTF-8去“结扎”它。

  一个J2EE应用系统需要做下列几步工作:

开发和编译代码时指定字符集为UTF-8。JBuilder和Eclipse都可以在项目属性中设置。
使用过滤器,如果所有请求都经过一个Servlet控制分配器,那么使用Servlet的filter执行语句,将所有来自浏览器的请求(request)转换为UTF-8,因为浏览器发过来的请求包根据浏览器所在的操作系统编码,可能是各种形式编码。关键一句:
request.setCharacterEncoding("UTF-8")。
网上有此filter的源码,Jdon框架源码中com.jdon.util.SetCharacterEncodingFilter
需要配置web.xml 激活该Filter。
在JSP头部声明:<%@ page contentType="text/html;charset= UTF-8" %>。
在Jsp的html代码中,声明UTF-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
设定数据库连接方式是UTF-8。例如连接MYSQL时配置URL如下:
jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
一般数据库都可以通过管理设置设定UTF-8
其他和外界交互时能够设定编码时就设定UTF-8,例如读取文件,操作XML等。
       
  以上讨论了Java/J2EE的中文问题。如果整个应用系统是从开始进行开发,那么统一指定编码为UTF-8就非常容易做到。如果是在英文源代码基础上二次开发,那么首先要将原来的源代码转换为统一编码UTF-8,那么这种转换工作会带来一定的麻烦。

  
  有了这个解决方案,无论使用什么框架Struts 或JSF或未来出现的Java技术,统一成UTF-8的方案都不会出现乱码,笔者以前在Jsp/Servlet时就基于这个原则,后来使用Struts等框架,从未被乱码困扰过,希望本方案公布出来供更多初学者分享,减少Java/J2EE的第一个拦路虎,也避免采取一些临时解决方案。


***********************************************************************************************************************
<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>
<servlet-name>action</servlet-name>
</filter-mapping>


***********************************************************************************************

hibernate+mysql写入数据库的中文是乱码,怎么解决??


hibernate.hbm.xml加上属性.
<property name=\"connection.useUnicode\">true</property>
<property name=\"connection.characterEncoding\">UTF-8</property>

mysql 的驱动用3.0.15以上版本的,

加个Filter, 使用UTF-8字符集就可以了,




1.使ApplicationResources.properties支持中文
建立一个ApplicationResources_ISO.properties文件,把应用程序用的message都写进去,然后在dos下执行这个命令,
native2ascii -encoding gb2312 ApplicationResources_ISO.properties ApplicationResources.properties
这样就会将ISO编码的ApplicationResources转换成GB2312编码的格式了,同时保存到ApplicationResources.properties.
native2ascii这个工具是jdk自带的一个东东,所以如果path都设定正确就可以直接运行了,你可以在$java_home$/bin下找到他。
转换后的中文类似于这个样子
iso 格式下 :tj.type=商品车类型
gb2312格式下 :tj.type=\u5546\u54c1\u8f66\u7c7b\u578b
然后在struts-config.xml中设置应用这个资源文件
<message-resources parameter=\"com.huahang.tj.ApplicationResources\" key=\"org.apache.struts.action.MESSAGE\" />
开发jsp时在jsp的开头写上<%@ page contentType=\"text/html; charset=gb2312\" %>,将字符集设置成gb2312就可以了。

2.使数据库操作支持中文。
数据库操作支持中文一直让我比较头痛,但是感谢善解人衣向我推荐了www.chinaxp.org,这个网站是用struts框架开发的,而且
开放源码,下载了源码后发现它的中文处理得很好,阅读部分源码,没有发现什么特殊的字符集转换,很纳闷,偶然看到楼上网友
留言知道原来servlet可以统一设置字符转换。chinaxp.org就是这么做的。
在web.xml中加上
   <filter>
     <filter-name>Set Character Encoding</filter-name>
     <filter-class>com.huahang.tj.struts.filters.SetCharacterEncodingFilter</filter-class>
     <init-param>
       <param-name>encoding</param-name>
       <param-value>GB2312</param-value>
     </init-param>
     <init-param>
       <param-name>ignore</param-name>
       <param-value>true</param-value>
     </init-param>
   </filter>
   <filter-mapping>
     <filter-name>Set Character Encoding</filter-name>
     <servlet-name>action</servlet-name>
   </filter-mapping>
这里会涉及一个bean,源码如下:
/*
* XP Forum
*   
* Copyright (c) 2002-2003 RedSoft Group.   All rights reserved.
*
*/
package com.huahang.tj.struts.filters;

import javax.servlet.*;
import java.io.IOException;

/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding.   Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
*      for this request, either conditionally or unconditionally based on
*      the <code>ignore</code> initialization parameter.   This parameter
*      is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to \"true\", any character encoding
*      specified by the client is ignored, and the value returned by the
*      <code>selectEncoding()</code> method is set.   If set to \"false,
*      <code>selectEncoding()</code> is called <strong>only</strong> if the
*      client has not already specified an encoding.   By default, this
*      parameter is set to \"true\".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user\'s session.</p>
*
* @author <a href=\"mailto:jwtronics@yahoo.com\">John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {

     // ----------------------------------------------------- Instance Variables


     /**
      * The default character encoding to set for requests that pass through
      * this filter.
      */
     protected String encoding = null;


     /**
      * The filter configuration object we are associated with.   If this value
      * is null, this filter instance is not currently configured.
      */
     protected FilterConfig filterConfig = null;


     /**
      * Should a character encoding specified by the client be ignored?
      */
     protected boolean ignore = true;


     // --------------------------------------------------------- Public Methods


     /**
      * Take this filter out of service.
      */
     public void destroy() {

         this.encoding = null;
         this.filterConfig = null;

     }


     /**
      * Select and set (if specified) the character encoding to be used to
      * interpret request parameters for this request.
      *
      * @param request The servlet request we are processing
      * @param result The servlet response we are creating
      * @param chain The filter chain we are processing
      *
      * @exception IOException if an input/output error occurs
      * @exception ServletException if a servlet error occurs
      */
     public void doFilter(ServletRequest request, ServletResponse response,
                          FilterChain chain)
     throws IOException, ServletException {

         // Conditionally select and set the character encoding to be used
         if (ignore || (request.getCharacterEncoding() == null)) {
             String encoding = selectEncoding(request);
             if (encoding != null)
                 request.setCharacterEncoding(encoding);
         }

     // Pass control on to the next filter
         chain.doFilter(request, response);

     }


     /**
      * Place this filter into service.
      *
      * @param filterConfig The filter configuration object
      */
     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 if (value.equalsIgnoreCase(\"true\"))
             this.ignore = true;
         else if (value.equalsIgnoreCase(\"yes\"))
             this.ignore = true;
         else
             this.ignore = false;

     }


     // ------------------------------------------------------ Protected Methods


     /**
      * Select an appropriate character encoding to be used, based on the
      * characteristics of the current request and/or filter initialization
      * parameters.   If no character encoding should be set, return
      * <code>null</code>.
      * <p>
      * The default implementation unconditionally returns the value configured
      * by the <strong>encoding</strong> initialization parameter for this
      * filter.
      *
      * @param request The servlet request we are processing
      */
     protected String selectEncoding(ServletRequest request) {

         return (this.encoding);

     }

}//EOC
加上这个后,在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了。
但是这个好像需要servlet 2.2以上的容器



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics