网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  struts中用plugin扩展hibernate的例子     
  文章作者:未知  文章来源:水木森林  
  查看:81次  录入:管理员--2007-11-17  
 
  使用struts的plugin技术把hibernatesessionfactory,过程如下:
  
  (1) 创建hibernatesessionfactory.java,代码如下:
  package zy.pro.td.util;
  
  import net.sf.hibernate.hibernateexception;
  import net.sf.hibernate.session;
  import net.sf.hibernate.cfg.configuration;
  
  /**
  * configures and provides access to hibernate sessions, tied to the
  * current thread of execution. follows the thread local session
  * pattern, see {@link http://hibernate.org/42.html}.
  */
  public class hibernatesessionfactory {
  
  /**
  * location of hibernate.cfg.xml file.
  * notice: location should be on the classpath as hibernate uses
  * #resourceasstream style lookup for its configuration file. that
  * is place the config file in a java package - the default location
  * is the default java package.<br><br>
  * examples: <br>
  * <code>config_file_location = "/hibernate.conf.xml".
  * config_file_location = "/com/foo/bar/myhiberstuff.conf.xml".</code>
  */
  private static string config_file_location = "/hibernate.cfg.xml";
  
  /** holds a single instance of session */
  private final threadlocal threadlocal = new threadlocal();
  
  /** the single instance of hibernate configuration */
  private final configuration cfg = new configuration();
  
  /** the single instance of hibernate sessionfactory */
  private net.sf.hibernate.sessionfactory sessionfactory;
  
  /**
  * returns the threadlocal session instance. lazy initialize
  * the <code>sessionfactory</code> if needed.
  *
  * @return session
  * @throws hibernateexception
  */
  public session currentsession() throws hibernateexception {
  session session = (session) threadlocal.get();
  
  if (session == null) {
  if (sessionfactory == null) {
  try {
  cfg.configure(config_file_location);
  sessionfactory = cfg.buildsessionfactory();
  }
  catch (exception e) {
  system.err.println("%%%% error creating sessionfactory %%%%");
  e.printstacktrace();
  }
  }
  session = sessionfactory.opensession();
  threadlocal.set(session);
  }
  
  return session;
  }
  
  /**
  * close the single hibernate session instance.
  *
  * @throws hibernateexception
  */
  public void closesession() throws hibernateexception {
  session session = (session) threadlocal.get();
  threadlocal.set(null);
  
  if (session != null) {
  session.close();
  }
  }
  
  /**
  * default constructor.
  */
  public hibernatesessionfactory() {
  }
  
  }
  
  (2) 创建hibernateplugin.java,代码如下:
  package zy.pro.td.plugin;
  
  /*
  * created on oct 4, 2004
  *
  * to change the template for this generated file go to
  * window>preferences>java>code generation>code and comments
  */
  import javax.servlet.servletexception;
  
  import org.apache.struts.action.actionservlet;
  import org.apache.struts.action.plugin;
  import org.apache.struts.config.moduleconfig;
  
  import javax.naming.context;
  import javax.naming.initialcontext;
  
  import zy.pro.td.util.hibernatesessionfactory;
  
  /**
  * @author sunil
  *
  *  this class will initialize hibernate and bind sessionfactory in jndi at the
  *  time of application and startup and unbind it from jndi at the time of application
  * shutdown
  */
  public class hibernateplugin
  implements plugin {
  
  private static final string jndi_hibernate = "jndi_hibernate_factory";
  private  hibernatesessionfactory hsf;
  private string name;
  
  public hibernateplugin() {
  hsf=new hibernatesessionfactory();
  }
  
  // this method will be called at the time of application shutdown
  public void destroy() {
  system.out.println("entering hibernateplugin.destroy()");
  //put hibernate cleanup code here
  system.out.println("exiting hibernateplugin.destroy()");
  }
  
  //this method will be called at the time of application startup
  public void init(actionservlet actionservlet, moduleconfig config) throws
  servletexception {
  system.out.println("entering hibernateplugin.init()");
  system.out.println("value of init parameter " + getname());
  //uncomment next two lines if you want to throw unavailableexception from your servlet
  //    if(true)
  //      throw new servletexception("error configuring hibernateplugin");
  system.out.println("exiting hibernateplugin.init()");
  bindfactorytojndi();
  }
  
  private void bindfactorytojndi() {
  
  try {
  context ctx = new initialcontext();
  ctx.bind(this.jndi_hibernate,hsf);
  system.out.println("bindind the hibernate factory to jndi successfully");
  }
  catch (exception e) {
  e.printstacktrace();
  }
  }
  
  public string getname() {
  
  return name;
  }
  
  public void setname(string string) {
  name = string;
  }
  }
  
  (3) 配置struts-config.xml,如下:
  <?xml version="1.0" encoding="utf-8"?>
  <!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  <struts-config>
  <form-beans>
  <form-bean name="useractionform" type="zy.pro.td.controller.useractionform" />
  </form-beans>
  <action-mappings>
  <action name="useractionform" path="/act/log/login" scope="request" type="zy.pro.td.controller.loginaction" />
  </action-mappings>
  <plug-in classname="org.apache.struts.tiles.tilesplugin">
  <set-property property="definitions-config" value="/web-inf/tiles-defs.xml" />
  </plug-in>
  <plug-in classname="org.apache.struts.validator.validatorplugin">
  <set-property property="pathnames" value="/web-inf/validator-rules.xml,/web-inf/validation.xml" />
  </plug-in>
  <plug-in classname="zy.pro.td.plugin.hibernateplugin" />
  <plug-in classname="zy.pro.td.plugin.hibernatesessionfactoryplugin" />
  </struts-config>
  
  这一部分就是你的嵌入代码
  
  (4)创建actionform,代码如下:
  package zy.pro.td.controller;
  
  import org.apache.struts.action.*;
  import javax.servlet.http.*;
  
  public class useractionform extends actionform {
  private string password;
  private string username;
  public string getpassword() {
  return password;
  }
  public void setpassword(string password) {
  this.password = password;
  }
  public string getusername() {
  return username;
  }
  public void setusername(string username) {
  this.username = username;
  }
  public actionerrors validate(actionmapping actionmapping, httpservletrequest httpservletrequest) {
  /**@todo: finish this method, this is just the skeleton.*/
  return null;
  }
  public void reset(actionmapping actionmapping, httpservletrequest httpservletrequest) {
  }
  }
  
  (5)创建action
  
  package zy.pro.td.controller;
  
  import org.apache.struts.action.*;
  import javax.servlet.http.*;
  import javax.naming.context;
  import javax.naming.initialcontext;
  import net.sf.hibernate.sessionfactory;
  import net.sf.hibernate
 
 
上一篇: 发现hibernate中文问题又一解决方法    下一篇: 源代码分享——进化中hibernate3脚本
  相关文档
体验java 1.5中面向(aop)编程 11-16
使用jdbc创建数据库对象 11-17
什么是aop系列之二:aop与权限控制实现 11-17
使用ant编译打包应用程序 11-17
谈谈j2se中的序列化之当序列化遭遇继承 11-16
sun scjp考试心得 11-16
java嵌入式开发讲座 第二讲 11-17
发挥j2ee的优势,管理j2ee的世界(上) 11-17
java se 6中的awt模态增强功能 11-17
抽象类和接口-实战练习 11-17
国外专家谈游戏制作 11-17
sun jes服务器软件已支持更多操作系统 11-17
关于eclipse插件开发之定制向导(图) 11-17
jbuilder2005 servlet开发之过滤器 11-16
理解subjects, principals and credentials 11-17
基本对象池的使用 11-17
javascript在e-mail中的应用 11-17
用.net的system.globalization来创建多语言应用程序 11-17
中国移动:开启“无线java”之门 11-17
java学习推荐书籍 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息