熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

如何在Spring中解決中文亂碼的方法

2022-06-13   來源: Java開源技術 

  軟件環境JDK_+Eclipse+MS SQL SERVER+SP+JTDS+Struts+Hibernate+Spring
  
  由於剛開始學習這個Framework所以很多東西也不是特別清楚以前在JB環境下也沒怎麼遇到亂碼問題這次試了很多方法都不行於是決定加個Fileter了webxml部分內容如下
  
  <filter>
  <filtername>SetCharacterEncoding</filtername>
  <filterclass>
  orgspringframeworkwebfilterCharacterEncodingFilter</filterclass>
  <initparam>
  <paramname>encoding</paramname>
  <paramvalue>GBK</paramvalue>
  </initparam>
  </filter>
  <! 要過濾得類型 >
  <filtermapping>
  <filtername>SetCharacterEncoding</filtername>
  <urlpattern>*jsp</urlpattern>
  </filtermapping>
  
  通過在Action中加斷點調試發現使用超連接的跳轉是可以使用Filter的但是如果是以do為後綴的請求就不行了抱著試試看的心理我修改了webxml
  
  <?xml version= encoding=UTF?>
  <webapp>
  <!Spring ApplicationContext>
  <servlet>
  <servletname>context</servletname>
  <servletclass> orgsprntextContextLoaderServlet
  </servletclass>
  <loadonstartup></loadonstartup>
  </servlet>
  <servlet>
  <servletname>action</servletname>
  <servletclass>orgapachestrutsactionActionServlet</servletclass>
  <initparam>
  <paramname>config</paramname>
  <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
  </initparam>
  <initparam>
  <paramname>debug</paramname>
  <paramvalue></paramvalue>
  </initparam>
  <initparam>
  <paramname>detail</paramname>
  <paramvalue></paramvalue>
  </initparam>
  <loadonstartup></loadonstartup>
  </servlet>
  <servletmapping>
  <servletname>action</servletname>
  <urlpattern>*do</urlpattern>
  </servletmapping>
  <filter>
  <filtername>SetCharacterEncoding</filtername>
  <filterclass>
  orgspringframeworkwebfilterCharacterEncodingFilter</filterclass>
  <initparam>
  <paramname>encoding</paramname>
  <paramvalue>GBK</paramvalue>
  </initparam>
  </filter>
  <! 要過濾得類型 >
  <filtermapping>
  <filtername>SetCharacterEncoding</filtername>
  <urlpattern>*jsp</urlpattern>
  </filtermapping>
  <filtermapping>
  <filtername>SetCharacterEncoding</filtername>
  <urlpattern>*do</urlpattern>
  </filtermapping>
  <welcomefilelist>
  <welcomefile>mainjsp</welcomefile>
  </welcomefilelist>
  <taglib>
  <tagliburi>/WEBINF/strutsbeantld</tagliburi>
  <tagliblocation>/WEBINF/strutsbeantld</tagliblocation>
  </taglib>
  <taglib>
  <tagliburi>/WEBINF/strutshtmltld</tagliburi>
  <tagliblocation>/WEBINF/strutshtmltld</tagliblocation>
  </taglib>
  <taglib>
  <tagliburi>/WEBINF/strutslogictld</tagliburi>
  <tagliblocation>/WEBINF/strutslogictld</tagliblocation>
  </taglib>
  <taglib>
  <tagliburi>/WEBINF/strutstemplatetld</tagliburi>
  <tagliblocation>/WEBINF/strutstemplatetld</tagliblocation>
  </taglib>
  <taglib>
  <tagliburi>/WEBINF/strutstilestld</tagliburi>
  <tagliblocation>/WEBINF/strutstilestld</tagliblocation>
  </taglib>
  <taglib>
  <tagliburi>/WEBINF/strutsnestedtld</tagliburi>
  <tagliblocation>/WEBINF/strutsnestedtld</tagliblocation>
  </taglib>
  </webapp>
  
  主要在這裡多加了一個過濾內容!其他的為防止萬一在頁面(jsp)上也加了些東西
  
  <%@ page contentType=text/html; charset=GBK pageEncoding=GBK%>
  
  <meta httpequiv=contenttype content=text/html; charset=GBK>
  
  呵呵可以說是武裝到牙齒了開始調試這次在Debug的時候顯示出從頁面中傳來的值終於不是亂碼了保存在數據庫中後也不是亂碼這個問題目前是部分解決了因為我還沒有測試在頁面上哪些是不用寫的還有就是頁面回現漢字是是否會有問題不過這裡先把自己的所得記錄下來如果有高人就此事談論過就算我孤陋寡聞吧呵呵
  
  另外給出我的Hibernatecfgxml的部分內容
  
  <sessionfactory>
  <property name=nnectionurl>jdbc:jtds:sqlserver://:;DatabaseName=HomeConsume;charset=GBK</property>
  <property name=hibernatecglibuse_reflection_optimizer>true</property>
  <property name=nnectionpassword>sju</property>
  <property name=nnectionusername>sa</property>
  <property name=nnectiondriver_class>netsourceforgejtdsjdbcDriver</property>
  <property name=hibernatedialect>orghibernatedialectSQLServerDialect</property>
  <mapping resource=net/magicyang/homeconsume/pojo/Testhbmxml />
  <mapping resource=net/magicyang/homeconsume/pojo/Consumeinfohbmxml />
  <mapping resource=net/magicyang/homeconsume/pojo/Consumetypehbmxml />
  </sessionfactory>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27891.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.