软件环境: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>
同时欢迎各位与我讨论有关的问题,谢谢你们
新闻热点
疑难解答