Struts Hibernate-Integrationsbeispiel

Beispiel für die Integration von Struts + Hibernate

Ein Tutorial zur Integration von Hibernate in eine mit Apache Struts 1.x entwickelte Webanwendung.

Laden Sie es herunter -Struts-Hibernate-Example.zip

Schritte der Integration:

  1. Erstellen Sie eine neueHibernate Struts plug-in-Datei, um die Hibernate-Sitzungsfactory im Servlet-Kontext festzulegen, und fügen Sie diese Datei in diestruts-config.xml-Datei ein.

  2. Holen Sie sich in Struts dieHibernate session factory from servlet context und führen Sie die gewünschte Ruhezustand-Aufgabe aus.

1. Hibernate Struts Plug-In

Erstellen Sie ein Hibernate Struts-Plug-In, rufen Sie die Hibernate-Sitzungsfactory ab und speichern Sie sie im Servlet-Kontext für spätere Benutzer -servlet.getServletContext().setAttribute(KEY_NAME, factory);.

package com.example.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

Fügen Sie dieHibernate Struts plug-in in die Struts-Konfigurationsdatei (struts-config.xml) ein.


    ...
    
        
    
    ...

3. Laden Sie die Hibernate Session Factory herunter

In der Aktionsklasse Struts können Sie dieHibernate session factory from servlet context erhalten.

servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);

und erledigen Sie den Ruhezustand wie gewohnt.

package com.example.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.example.common.plugin.HibernatePlugin;
import com.example.customer.form.CustomerForm;
import com.example.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");

  }
}

Erledigt.