Apache Struts 1.xで開発されたWebアプリケーションでHibernateをどのように統合するかを示すチュートリアル。
ダウンロードする – リンク://wp-content/uploads/2010/04/Struts-Hibernate-Example.zip[Struts-Hibernate-Example.zip]
サーブレットコンテキスト内のセッションファクトリであり、このファイルを
struts-config.xml
ファイルに含めます。
-
Strutsでは、
Hibernateセッションファクトリをサーブレットコンテキスト
から取得し、
必要なHibernateタスクを実行します。
1. Hibernate Strutsプラグイン
HibernateのStrutsプラグインを作成し、Hibernateのセッションファクトリを取得し、後で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
-
Hibernate Strutsプラグイン
をStruts設定ファイル(
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. Hibernateセッションファクトリを取得する
Strutsアクションクラスでは、
Hibernateセッションファクトリをサーブレットコンテキスト
から取得できます。
servlet.getServletContext().getAttribute(HibernatePlugin.KEY__NAME);
通常どおりにHibernateタスクを実行します。
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"); } }
完了しました。