文章标题
2015-09-09 10:20
369 查看
A tutorial to show how to integrate Hibernate with in a web application developed with Apache Struts 1.x.
Steps of the integration :
Create a new Hibernate Struts plug-in file to set the Hibernate session factory in servlet context, and include this file in
In Struts, get the Hibernate session factory from servlet context, and do whatever Hibernate task you want.
and do whatever Hibernate task as normal.
Done.
Steps of the integration :
Create a new Hibernate Struts plug-in file to set the Hibernate session factory in servlet context, and include this file in
struts-config.xmlfile.
In Struts, get the Hibernate session factory from servlet context, and do whatever Hibernate task you want.
1. Hibernate Struts Plug-in
Create a Hibernate Struts Plug-in, get the Hibernate session factory, store it into the servlet context for later user –servlet.getServletContext().setAttribute(KEY_NAME, factory);.
package com.mkyong.common.plugin; import java.net.URL; import javax.servlet.ServletException; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernatePlugin implements PlugIn { private Configuration config; private SessionFactory factory; private String path = "/hibernate.cfg.xml"; private static Class clazz = HibernatePlugin.class; public static final String KEY_NAME = clazz.getName(); public void setPath(String path) { this.path = path; } public void init(ActionServlet servlet, ModuleConfig modConfig) throws ServletException { try { //save the Hibernate session factory into serlvet context URL url = HibernatePlugin.class.getResource(path); config = new Configuration().configure(url); factory = config.buildSessionFactory(); servlet.getServletContext().setAttribute(KEY_NAME, factory); } catch (MappingException e) { throw new ServletException(); } catch (HibernateException e) { throw new ServletException(); } } public void destroy() { try { factory.close(); } catch (HibernateException e) { e.printStackTrace(); } } }
2. struts-config.xml
Include the Hibernate Struts plug-in into the Struts configuration file (struts-config.xml).
<struts-config> ... <plug-in className="com.mkyong.common.plugin.HibernatePlugin"> <set-property property="path" value="/hibernate.cfg.xml"/> </plug-in> ... <struts-config>
3. Get the Hibernate session factory
In Struts action class, you can get the Hibernate session factory from servlet context.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
and do whatever Hibernate task as normal.
package com.mkyong.customer.action; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.mkyong.common.plugin.HibernatePlugin; import com.mkyong.customer.form.CustomerForm; import com.mkyong.customer.model.Customer; public class AddCustomerAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { SessionFactory sessionFactory = (SessionFactory) servlet.getServletContext() .getAttribute(HibernatePlugin.KEY_NAME); Session session = sessionFactory.openSession(); CustomerForm customerForm = (CustomerForm)form; Customer customer = new Customer(); //copy customerform to model BeanUtils.copyProperties(customer, customerForm); //save it customer.setCreatedDate(new Date()); session.beginTransaction(); session.save(customer); session.getTransaction().commit(); return mapping.findForward("success"); } }
Done.
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- java struts常见错误以及原因分析
- Struts之logic标签库详解
- SSH框架网上商城项目第20战之在线支付平台
- java中struts 框架的实现
- sprng和struts有什么区别?
- SpringMVC+MyBatis项目总结(三)
- MyEclipse7.5+JDK1.6+Tomcat6+Struts1.3+MySQL4.1开发演示
- struts使用拦截器注解
- 懒到极致:对mybatis的进一步精简
- 浅谈struts1和struts2框架(转载+整理)
- Maven搭建struts2+spring+hibernate环境(二)
- Maven搭建struts2+spring+hibernate环境(三)
- 国内首创Java开发者的技术体系
- Struts知识
- struts 获取客户端真实ip
- 转载--Struts1.1 validation framework 使用中的若干经验
- 初探Struts + Hibernate(二) --从HQL中取值
- Struts is History