軟件環(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>
同時歡迎各位與我討論有關的問題,謝謝你們
| 
 
 | 
新聞熱點
疑難解答