Struts Spring Hibernate統合の例

Struts + Spring + Hibernate統合の例

このStruts1.x + Spring + Hibernateの例をダウンロード–Struts-Spring-Hibernate-Example.zip

このチュートリアルでは、シンプルな顧客管理(追加および選択)Webアプリケーション、プロジェクト管理ツールとしてのMaven、WebフレームワークとしてのStruts 1.x、依存関係注入フレームワークとしてのSpring、およびデータベースORMフレームワークとしてのHibernateの作成方法を学びます。

全体的な統合アーキテクチャは次のようになります。

Struts (Web page) <---> Spring DI <--> Hibernate (DAO) <---> Database

これらすべてのテクノロジーを統合するには、次のようにする必要があります。

  1. Springの「LocalSessionFactoryBean」クラスを使用して、SpringとHibernateを統合します。

  2. Springの既製のStrutsプラグイン「ContextLoaderPlugIn」を介してSpringをStrutsと統合します。

1. プロジェクト構造

これがこの最終プロジェクト構造です。

struts-spring-hibernate-1

struts-spring-hibernate-2

2. テーブルスクリプト

顧客テーブルを作成して、顧客の詳細を保存します。

DROP TABLE IF EXISTS `example`.`customer`;
CREATE TABLE  `example`.`customer` (
  `CUSTOMER_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `NAME` varchar(45) NOT NULL,
  `ADDRESS` varchar(255) NOT NULL,
  `CREATED_DATE` datetime NOT NULL,
  PRIMARY KEY (`CUSTOMER_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

3. Mavenの詳細

pom.xmlですべてのStruts、Spring、Hibernate依存関係ライブラリを定義します。
pom.xml


  4.0.0
  com.example.common
  StrutsSpringExample
  war
  1.0-SNAPSHOT
  StrutsExample Maven Webapp
  http://maven.apache.org

  
    
        Java.Net
        http://download.java.net/maven/2/
    

    
        JBoss repository
        http://repository.jboss.com/maven2/
    

  

  

        
    
      org.springframework
      spring
      2.5.6
    

        
      org.springframework
      spring-web
      2.5.6
    

    
      org.springframework
      spring-struts
      2.0.8
    

        
    
      javax
      javaee-api
      6.0
    

        
        
          junit
          junit
          3.8.1
          test
        

        
        
          org.apache.struts
      struts-core
          1.3.10
        

        
          org.apache.struts
      struts-taglib
          1.3.10
        

        
          org.apache.struts
      struts-extras
          1.3.10
        

        
    
      mysql
      mysql-connector-java
      5.1.9
    

    
    
      org.hibernate
      hibernate
      3.2.7.ga
    

    
    
      dom4j
      dom4j
      1.6.1
    

    
      commons-logging
      commons-logging
      1.1.1
    

    
      commons-collections
      commons-collections
      3.2.1
    

    
      cglib
      cglib
      2.2
    
    

    
    
      antlr
      antlr
      2.7.7
    
    

  
  
    StrutsExample
  

4. 休止状態

Hibernateで構成する必要はほとんどありません。顧客のXMLマッピングファイルとモデルを宣言するだけです。
Customer.hbm.xml



    

        
            
            
        
        
            
        
        
            
        
        
            
        
    

Customer.java

package com.example.customer.model;

import java.util.Date;

public class Customer implements java.io.Serializable {

    private long customerId;
    private String name;
    private String address;
    private Date createdDate;

    //getter and setter methods

}

5. 春

ビジネスオブジェクト(BO)およびデータアクセスオブジェクト(DAO)のSpringのBean宣言。 DAOクラス(CustomerDaoImpl.java)は、Springの「HibernateDaoSupport」クラスを拡張して、Hibernate関数に簡単にアクセスできるようにします。
CustomerBean.xml



    
        
    

    
        
    

CustomerBo.java

package com.example.customer.bo;

import java.util.List;

import com.example.customer.model.Customer;

public interface CustomerBo{

    void addCustomer(Customer customer);

    List findAllCustomer();

}

CustomerBoImpl.java

package com.example.customer.bo.impl;

import java.util.List;

import com.example.customer.bo.CustomerBo;
import com.example.customer.dao.CustomerDao;
import com.example.customer.model.Customer;

public class CustomerBoImpl implements CustomerBo{

    CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    public void addCustomer(Customer customer){

        customerDao.addCustomer(customer);

    }

    public List findAllCustomer(){

        return customerDao.findAllCustomer();
    }
}

CustomerDao.java

package com.example.customer.dao;

import java.util.List;

import com.example.customer.model.Customer;

public interface CustomerDao{

    void addCustomer(Customer customer);

    List findAllCustomer();

}

CustomerDaoImpl.java

package com.example.customer.dao.impl;

import java.util.Date;
import java.util.List;

import com.example.customer.dao.CustomerDao;
import com.example.customer.model.Customer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CustomerDaoImpl extends
       HibernateDaoSupport implements CustomerDao{

    public void addCustomer(Customer customer){

        customer.setCreatedDate(new Date());
        getHibernateTemplate().save(customer);

    }

    public List findAllCustomer(){

        return getHibernateTemplate().find("from Customer");

    }
}

6. 春+休止状態

データベースの詳細を宣言し、「LocalSessionFactoryBean」を介してSpringとHibernateを統合します。
database.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/example
jdbc.username=root
jdbc.password=password

DataSource.xml



 
   
     WEB-INF/classes/config/database/properties/database.properties
   


  
    
    
    
    
  

HibernateSessionFactory.xml






    
      
    

    
       
         org.hibernate.dialect.MySQLDialect
         true
       
    

    
    
          com/example/customer/hibernate/Customer.hbm.xml
    
     


SpringBeans.xml



    
    
    

    
    

7. ストラット+スプリング

SpringをStrutsと統合するには、Springの組み込みStrutsプラグイン「ContextLoaderPlugIn」をstruts-config.xmlファイルに登録する必要があります。 Actionクラスでは、Springの「ActionSupport」クラスを拡張する必要があり、getWebApplicationContext()を介してSpringBeanを取得できます。

AddCustomerAction.java

package com.example.customer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;

import com.example.customer.bo.CustomerBo;
import com.example.customer.form.CustomerForm;
import com.example.customer.model.Customer;

public class AddCustomerAction extends ActionSupport{

public ActionForward execute(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response)
        throws Exception {

    CustomerBo customerBo =
      (CustomerBo) getWebApplicationContext().getBean("customerBo");

    CustomerForm customerForm = (CustomerForm)form;
    Customer customer = new Customer();

    //copy customerform to model
    BeanUtils.copyProperties(customer, customerForm);

    //save it
    customerBo.addCustomer(customer);

    return mapping.findForward("success");

  }
}

ListCustomerAction.java

package com.example.customer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.web.struts.ActionSupport;

import com.example.customer.bo.CustomerBo;
import com.example.customer.model.Customer;

public class ListCustomerAction extends ActionSupport{

  public ActionForward execute(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response)
        throws Exception {

    CustomerBo customerBo =
      (CustomerBo) getWebApplicationContext().getBean("customerBo");

    DynaActionForm dynaCustomerListForm = (DynaActionForm)form;

    List list = customerBo.findAllCustomer();

    dynaCustomerListForm.set("customerList", list);

    return mapping.findForward("success");

  }
}

CustomerForm.java

package com.example.customer.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class CustomerForm extends ActionForm {

    private String name;
    private String address;

    //getter and setter, basic validation

}

Customer.properties

#customer module label message
customer.label.name = Name
customer.label.address = Address

customer.label.button.submit = Submit
customer.label.button.reset = Reset

#customer module error message
customer.err.name.required = Name is required
customer.err.address.required = Address is required

add_customer.jsp

Struts + Spring + Hibernate example

Add Customer

:

:

list_customer.jsp

Struts + Spring + Hibernate example

List All Customers

顧客名

住所

+
+

struts-config.xml





    
        

        
              
        

    

    

     

    

        
    

    

        
    
      

    

    
    
        
    

web.xml




  Struts Hibernate Examples

  
    action
    
        org.apache.struts.action.ActionServlet
    
    
        config
        
         /WEB-INF/struts-config.xml
        
    
    1
  

  
       action
       *.do
  

8. デモンストレーション

1. List customer page
データベースからすべての顧客を一覧表示します。
http://localhost:8080/StrutsSpringExample/ListCustomer.do

struts-spring-hibernate-demo-1

2. Add customer page
顧客の詳細をデータベースに追加します。
http://localhost:8080/StrutsSpringExample/AddCustomerPage.do

struts-spring-hibernate-demo-2