软件环境:JDK1.4.2_09+Eclipse3.1+MS SQL SERVER200+SP3+JTDS1.0.2+Struts1.1+Hibernate3.0.5+Spring1.2.4。
由于刚开始学习这个Framework,所以很多东西也不是非凡清楚,以前在JB环境下也没怎么碰到乱码问题。这次试了很多方法都不行,于是决定加个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中加断点调试,发现使用超连接的跳转是可以使用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>