love fish大鵬一曰同風(fēng)起,扶搖直上九萬(wàn)里

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          friends

          link

          最新評(píng)論

          Struts+Hibernate中解決漢字編碼問(wèn)題(轉(zhuǎn))

          1. 首先所有的文字編碼均采用UTF-8格式,至于為什么要采用UTF-8.  
          <%@ page language="java" contentType="text/html; charset=utf-8" %>

          2. struts框架提供了資源信息文件,它包含了jsp頁(yè)面內(nèi)容的一些文字說(shuō)明,以及另一些供代碼中使用的信息輸出等(說(shuō)白了,就是些文字描述定義,可以當(dāng)成變量看待)。
          ApplicationResources.properties(默認(rèn)的是英文ApplicationResources.properties一份,命名為:ApplicationResources_zh_CN.properties 簡(jiǎn)體中文
          ApplicationResources_zh_TW.properties 繁體中文
           
          然后,執(zhí)行JDK自帶的命令如下:
          native2ascii -encoding gb2312 ApplicationResources_demo.properties ApplicationResources_zh_CN.properties
          3. struts中,對(duì)于中文參數(shù)的傳遞的文字編碼處理,一般加個(gè)過(guò)濾器類就可以了的。
          java源代碼如下:

          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;


          public class SetCharacterEncodingFilter implements Filter
          {
          ?protected String encoding = null;
          ?protected FilterConfig filterConfig = null;
          ?protected boolean ignore = true;
          ?
          ?public void destroy()
          ?{
          ??this.encoding = null;
          ??this.filterConfig = null;
          ?}

          ?public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
          ???throws IOException, ServletException
          ?{
          ??if (ignore || (request.getCharacterEncoding() == null))
          ??{
          ???String encoding = selectEncoding(request);
          ???if (encoding != null)
          ????request.setCharacterEncoding(encoding);
          ??}
          ??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 if (value.equalsIgnoreCase("true"))
          ???this.ignore = true;
          ??else if (value.equalsIgnoreCase("yes"))
          ???this.ignore = true;
          ??else
          ???this.ignore = false;
          ?}

          ?protected String selectEncoding(ServletRequest request)
          ?{
          ??return (this.encoding);
          ?}
          }
          4.? 配置web.xml文件

          ? 在<web-app>下加下面的代碼

          <filter>
          ?<filter-name>Set Character Encoding</filter-name>
          ?<filter-class>com.zxjsoft.util.SetCharacterEncodingFilter</filter-class>
          ?<init-param>
          ??<param-name>encoding</param-name>
          ??<param-value>utf-8</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>

          5.對(duì)于hibernate如說(shuō) 還得配置hibernate.cfg.xml文件
          ?在文件里加入
          ?<property name="hibernate.connection.useUnicode">true</property>
          ?<property name="hibernate.connection.characterEncoding">UTF-8(或者gb2312)</property>

          posted on 2007-03-23 09:42 liaojiyong 閱讀(607) 評(píng)論(0)  編輯  收藏 所屬分類: Struts

          主站蜘蛛池模板: 通河县| 南城县| 灌阳县| 乌鲁木齐市| 宽城| 九寨沟县| 五莲县| 翁牛特旗| 电白县| 历史| 巴彦淖尔市| 凤山县| 图木舒克市| 忻州市| 三江| 香港 | 漳浦县| 奈曼旗| 合肥市| 开鲁县| 衡水市| 奉新县| 抚顺县| 绍兴县| 延川县| 醴陵市| 庆云县| 北辰区| 九龙坡区| 辽宁省| 太保市| 甘肃省| 绥滨县| 昌都县| 搜索| 新干县| 衡南县| 靖边县| 临江市| 无锡市| 广昌县|