浏览 6509 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2003-09-24
http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg01715.html 我得实现方式是:Struts+DAO+hibernate. 另外,为了解决中文问题,实现了一个filter类,来设置request的encoding. 所有的Action都用filter过滤. 在没有用ThreadLocal管理之前,我是在DAO里,每个方法都开一个session,完了之后关闭,这样没有出现中文问题. 但是参照以上网址修改我得程序,主要是把我得SetCharacterEncodingFilter类修改成在filter里管理session. 我在jsp页面用 <%@ page contentType="text/html; charset=gb2312" %> 但是这样取出的中文都变成????.改为iso8859_1编码,输出正常. 不知上面是什末原因,请大家指教,先谢了! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2003-09-24
在对程序逐层分析之后,问题范围也逐步缩小了:
我的运行环境为:win2000,resin2.1.6,mysql3.23.56,hibernate2.0.1 以下源码是我的程序,: package com.dao.mysql; // EXPLICIT IMPORTS import com.dao.UserDAO; import com.persistent.User; import net.sf.hibernate.*; import net.sf.hibernate.cfg.Configuration; import java.util.Collection; import java.util.List; import java.util.ArrayList; /** * UserDAOImpl.java Description: * * @see * @author <a href="mailto:udbihcel@163.com">Steven Zheng</a>. * @version 1.0 */ public class UserDAOImpl1 { // Constants------------------------------- // Attributes------------------------------ private static SessionFactory sessionFactory; private static Configuration conf = null; // Static---------------------------------- public UserDAOImpl1(); throws Exception{ init();; } // Public Methods---------------------------------- /** * 根据User的ID取出user对象 * @param user 操作员的持久类. * @return User 如果成功,返回带有信息的user, * 否则返回null。 * @throws HibernateException */ public User findByUserID(User user); throws HibernateException{ try{ session = sessionFactory.openSession();; user = (User);session.load(User.class,user.getUserId(); );; /****************************************** System.err.println(user.getUserName(););; //在此取出中文的userName /****************************************** session.flush();; }catch(HibernateException he);{ user = null; throw he; //抛出异常 } return user; } //Private----------------------------------- //初始化sessionFactory private static synchronized void init(); throws Exception { if (sessionFactory != null); { //如果已经初始化,返回. return; } try { /************************************************* //如果在次全部载入*.hbm.xml文件,classpath已经有hibernate.cfg.xml文件 //会出现上面findByUserID(User user);取出的中文userName为????乱码. //数据库中为正常中文. conf = new Configuration();.configure();; /************************************************* /************************************************* //如果只是单独载入User,则取出的中文userName正常显示 conf = new Configuration();.addClass(User.class);; /************************************************* sessionFactory = conf.buildSessionFactory();; } catch (MappingException me); { throw me; } catch (HibernateException he); { throw he; } } private Transaction tx = null; private Session session = null; } ******************************************************* 以下贴出hibernate.cfg.xml文件内容: <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <!-- a SessionFactory instance listed as /jndi/name --> <session-factory name="hibernate/SessionFactory"> <!-- properties --> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="use_outer_join">false</property> <!-- database properties --> <property name="connection.datasource">java:comp/env/jdbc/mysql</property> <!-- mapping files --> <mapping resource="com/persistent/Boatbyday.hbm.xml" /> <mapping resource="com/persistent/City.hbm.xml" /> <mapping resource="com/persistent/Citysum.hbm.xml" /> <mapping resource="com/persistent/Deceivesurvey.hbm.xml" /> <mapping resource="com/persistent/Enter.hbm.xml" /> <mapping resource="com/persistent/Enterprise.hbm.xml" /> <mapping resource="com/persistent/Enterprisetype.hbm.xml" /> <mapping resource="com/persistent/Putupbyday.hbm.xml" /> <mapping resource="com/persistent/Sight.hbm.xml" /> <mapping resource="com/persistent/Sightbyday.hbm.xml" /> <mapping resource="com/persistent/Sightputday1.hbm.xml" /> <mapping resource="com/persistent/Sightputday2.hbm.xml" /> <mapping resource="com/persistent/Sightsum.hbm.xml" /> <mapping resource="com/persistent/Trafficbyday.hbm.xml" /> <mapping resource="com/persistent/Travelbyday.hbm.xml" /> <mapping resource="com/persistent/User.hbm.xml" /> </session-factory> </hibernate-configuration> ************************************************************ 另外,如果用ThreadLocal+filter来管理session,session是不是不用在每个DAO方法中关闭. 而是象http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg01715.html 中所介绍的,在filter中,在doFilter()方法之后来由ThreadLocal来管理关闭session. 在此先谢了!!! |
|
返回顶楼 | |
发表时间:2003-09-24
这应该和如何用hibernate无关吧,只和你的database connection或datasource的encoding有关吧。
|
|
返回顶楼 | |
发表时间:2003-09-24
谢谢你的回复:)
请看我的web.xml文件: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <servlet-name>action</servlet-name> </filter-mapping> <resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <init-param driver-name="org.gjt.mm.mysql.Driver"/> <init-param url="jdbc:mysql://172.16.1.145/goldenweek?useUnicode=true;characterEncoding=GBK"/> <init-param user="root"/> <init-param password=""/> <init-param max-connections="20"/> <init-param max-idle-time="30"/> </resource-ref> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources_zh</param-value> </init-param> <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>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>validate</param-name> <param-value>true</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> 因为所有的操作是先提交到Action中,中间用了filter,主要是设置request的encoding为gb2312. 连接数据库时,也指定了编码jdbc:mysql://172.16.1.145/goldenweek?useUnicode=true&characterEncoding=GBK 数据库中中文正常显示. |
|
返回顶楼 | |
发表时间:2003-09-30
crazyren,你好,我一直这样用,没有问题,能把你的错误贴出来吗?
|
|
返回顶楼 | |
发表时间:2003-10-09
昨天,问题得到解决了,主要问题出在hibernate.cfg.xml文件中.
在我上面的帖子中,hibernate.cfg.xml内容有以下信息: java:comp/env/jdbc/mysql数据源是在应用的web.xml文件中定义的. <!-- database properties --> <property name="connection.datasource">java:comp/env/jdbc/mysql</property> 把此行信息删除,把web.xml中的数据源定义也删除掉. 重启resin服务,问题解决. 谢谢大家的解答. |
|
返回顶楼 | |
发表时间:2003-10-09
两个地方的数据源定义都删除了,那你的数据源定义在哪里呢?
|
|
返回顶楼 | |