国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

我在Spring中解決中文亂碼的方法

2024-07-21 02:15:07
字體:
來源:轉載
供稿:網友

軟件環(huán)境:jdk1.4.2_09+eclipse3.1+ms sql server200+sp3+jtds1.0.2+struts1.1+hibernate3.0.5+spring1.2.4。

由于剛開始學習這個framework,所以很多東西也不是特別清楚,以前在jb環(huán)境下也沒怎么遇到亂碼問題。這次試了很多方法都不行,于是決定加個fileter了,web.xml部分內容如下:

 <filter>
  <filter-name>setcharacterencoding</filter-name>
  <filter-class>
   org.springframework.web.filter.characterencodingfilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>gbk</param-value>
  </init-param>
 </filter>
 <!-- 要過濾得類型 -->
 <filter-mapping>
  <filter-name>setcharacterencoding</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>

通過在action中加斷點調試,發(fā)現使用超連接的跳轉是可以使用filter的;但是如果是以.do為后綴的請求就不行了,抱著試試看的心理,我修改了web.xml:

<?xml version="1.0" encoding="utf-8"?>
<web-app>
 <!--spring applicationcontext-->
 <servlet>
  <servlet-name>context</servlet-name>
  <servlet-class> org.springframework.web.context.contextloaderservlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.actionservlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/web-inf/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>3</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>0</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <filter>
  <filter-name>setcharacterencoding</filter-name>
  <filter-class>
   org.springframework.web.filter.characterencodingfilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>gbk</param-value>
  </init-param>
 </filter>
 <!-- 要過濾得類型 -->
 <filter-mapping>
  <filter-name>setcharacterencoding</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>setcharacterencoding</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>

 <welcome-file-list>
  <welcome-file>main.jsp</welcome-file>
 </welcome-file-list>
 <taglib>
  <taglib-uri>/web-inf/struts-bean.tld</taglib-uri>
  <taglib-location>/web-inf/struts-bean.tld</taglib-location>
 </taglib>
 <taglib>
  <taglib-uri>/web-inf/struts-html.tld</taglib-uri>
  <taglib-location>/web-inf/struts-html.tld</taglib-location>
 </taglib>
 <taglib>
  <taglib-uri>/web-inf/struts-logic.tld</taglib-uri>
  <taglib-location>/web-inf/struts-logic.tld</taglib-location>
 </taglib>
 <taglib>
  <taglib-uri>/web-inf/struts-template.tld</taglib-uri>
  <taglib-location>/web-inf/struts-template.tld</taglib-location>
 </taglib>
 <taglib>
  <taglib-uri>/web-inf/struts-tiles.tld</taglib-uri>
  <taglib-location>/web-inf/struts-tiles.tld</taglib-location>
 </taglib>
 <taglib>
  <taglib-uri>/web-inf/struts-nested.tld</taglib-uri>
  <taglib-location>/web-inf/struts-nested.tld</taglib-location>
 </taglib>
</web-app>

主要在這里多加了一個過濾內容!其他的,為防止萬一,在頁面(jsp)上也加了些東西:

<%@ page contenttype="text/html; charset=gbk" pageencoding="gbk"%>

<meta http-equiv="content-type" content="text/html; charset=gbk">

呵呵,可以說是武裝到牙齒了,開始調試:這次在debug的時候,顯示出從頁面中傳來的值終于不是亂碼了,保存在數據庫中后,也不是亂碼。這個問題目前是部分解決了。因為我還沒有測試在頁面上哪些是不用寫的,還有就是頁面回現漢字是是否會有問題,不過這里先把自己的所得記錄下來,如果有高人就此事談論過,就算我孤陋寡聞吧,呵呵。

另外給出我的hibernate.cfg.xml的部分內容:

    <session-factory>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.0.3:1433;databasename=homeconsume;charset=gbk</property>
        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="hibernate.connection.password">sju</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.sqlserverdialect</property>
        <mapping resource="net/magicyang/homeconsume/pojo/test.hbm.xml" />
        <mapping resource="net/magicyang/homeconsume/pojo/consumeinfo.hbm.xml" />
        <mapping resource="net/magicyang/homeconsume/pojo/consumetype.hbm.xml" />
    </session-factory>

同時歡迎各位與我討論有關的問題,謝謝你們

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新竹市| 太康县| 东港市| 济阳县| 新宁县| 琼中| 弋阳县| 太谷县| 大姚县| 平塘县| 新源县| 桂阳县| 宁阳县| 德庆县| 临海市| 河北省| 灵山县| 靖江市| 屏边| 托克托县| 云南省| 柳江县| 嘉义县| 徐州市| 德庆县| 固始县| 旺苍县| 辛集市| 额敏县| 高台县| 泾源县| 油尖旺区| 巫山县| 昌图县| 克拉玛依市| 西畴县| 孟州市| 方山县| 盐城市| 洞口县| 彝良县|