鍍金池/ 教程/ Java/ Struts2+Spring+Hibernate集成實(shí)例
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫訪問
Struts2上傳多個(gè)文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實(shí)例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個(gè)復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗(yàn)證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實(shí)例
Struts2 &lt;sx:autocompleter&gt;自動(dòng)提示例子
Struts2 action標(biāo)簽
autocompleter自動(dòng)完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動(dòng)選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動(dòng)作的后綴擴(kuò)展名
Struts2 Hello World 實(shí)例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實(shí)例
如何獲取ServletContext對(duì)象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實(shí)例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫攔截器參數(shù)
多個(gè)Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實(shí)例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實(shí)例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國際化(i18n)
Chinese 本地化問題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實(shí)例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動(dòng)作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實(shí)例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實(shí)例
Struts2開發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對(duì)象
Struts2 主題和模板
Struts2自動(dòng)選擇下拉框的值
Struts2下載文件實(shí)例
在Struts2中獲取 HttpServletRequest
級(jí)聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個(gè)配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動(dòng)作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實(shí)例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個(gè)復(fù)選框缺省值
Struts2 基于XML校驗(yàn)
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實(shí)例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動(dòng)實(shí)例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實(shí)例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實(shí)例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動(dòng)作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對(duì)象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

Struts2+Spring+Hibernate集成實(shí)例

在本教程中,它顯示的集成 “Struts2 + Spring + Hibernate“,請(qǐng)務(wù)必檢查以下之前繼續(xù)學(xué)習(xí)教程。
  1. Struts2 + Hibernate集成實(shí)例
  2. Struts2 + Spring 集成實(shí)例
參見集成步驟總結(jié):
  1. 獲取所有的依賴庫(很多)。
  2. 注冊(cè) Spring 的 ContextLoaderListener 來整合 Struts2 和 Spring。
  3. 使用 Spring 的 LocalSessionFactoryBean 來集成 Spring 和 Hibernate。
  4. 完成所有連接。
請(qǐng)參閱它們之的關(guān)系:
Struts 2 <-- (ContextLoaderListener) --> Spring <-- (LocalSessionFactoryBean) --> Hibernate
這將是一個(gè)很長的教程,相關(guān)解釋并不是很多,請(qǐng)務(wù)必閱讀上述2篇文章的詳細(xì)情況說明以方面學(xué)習(xí)。

這將要?jiǎng)?chuàng)建一個(gè)客戶頁面,以添加客戶和列表的自定義函數(shù)。前端使用Struts2顯示,Spring作為依賴注入引擎,而 Hibernate 用來執(zhí)行數(shù)據(jù)庫操作。讓我們開始...

1. 工程文件夾結(jié)構(gòu)

在本章中,我們創(chuàng)建一個(gè) ssh 的web工程,工程的目錄結(jié)構(gòu)如下圖所示:

2. MySQL表結(jié)構(gòu)結(jié)構(gòu)

客戶(customer)表腳本。
DROP TABLE IF EXISTS `yiibai`.`customer`;
CREATE TABLE  `yiibai`.`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=17 DEFAULT CHARSET=utf8;

3. Hibernate相關(guān)配置

只有模型和映射文件是必需的,因?yàn)檫@里要用Spring處理Hibernate配置。

Customer.java – 創(chuàng)建客戶表對(duì)應(yīng)的一個(gè)類。

package com.yiibai.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
}

Customer.hbm.xml – Hibernate的客戶映射文件。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Julai 2010 11:40:18 AM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping>
    <class name="com.yiibai.customer.model.Customer" 
		table="customer" catalog="yiibai">
        <id name="customerId" type="java.lang.Long">
            <column name="customer_id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" not-null="true" />
        </property>
        <property name="address" type="string">
            <column name="address" not-null="true" />
        </property>
        <property name="createdDate" type="timestamp">
            <column name="create_date" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

5. Struts2相關(guān)

實(shí)現(xiàn)了 Bo 和 DAO 設(shè)計(jì)模式。所有Bo和DAO將由Spring Spring bean配置文件注入。在DAO中,讓它擴(kuò)展Spring的HibernateDaoSupport來集成 Spring 和 Hibernate。

CustomerBo.java

package com.yiibai.customer.bo;

import java.util.List;
import com.yiibai.customer.model.Customer;
 
public interface CustomerBo{
	
	void addCustomer(Customer customer);
	List<Customer> listCustomer();
	
}

CustomerBoImpl.java

package com.yiibai.customer.bo.impl;

import java.util.List;
import com.yiibai.customer.bo.CustomerBo;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer;
 
public class CustomerBoImpl implements CustomerBo{
	
	CustomerDAO customerDAO;
	//DI via Spring
	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	//call DAO to save customer
	public void addCustomer(Customer customer){
		customerDAO.addCustomer(customer);
	}
	
	//call DAO to return customers
	public List<Customer> listCustomer(){
		return customerDAO.listCustomer();
	}
}

CustomerDAO.java

package com.yiibai.customer.dao;

import java.util.List;
import com.yiibai.customer.model.Customer;
 
public interface CustomerDAO{
	
	void addCustomer(Customer customer);
	List<Customer> listCustomer();	
	
}

CustomerDAOImpl.java

package com.yiibai.customer.dao.impl;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yiibai.customer.dao.CustomerDAO;
import com.yiibai.customer.model.Customer;
 
public class CustomerDAOImpl extends HibernateDaoSupport 
    implements CustomerDAO{
	
	//add the customer
	public void addCustomer(Customer customer){
		getHibernateTemplate().save(customer);
	}
	
	//return all the customers in list
	public List<Customer> listCustomer(){
		return getHibernateTemplate().find("from Customer");		
	}
	
}

CustomerAction.java – Struts2 的動(dòng)作不再需要擴(kuò)展ActionSupport,它將由 Spring 來處理。

package com.yiibai.customer.action;

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

import com.yiibai.customer.bo.CustomerBo;
import com.yiibai.customer.model.Customer;
import com.opensymphony.xwork2.ModelDriven;
 
public class CustomerAction implements ModelDriven{

	Customer customer = new Customer();
	List<Customer> customerList = new ArrayList<Customer>();
	
	CustomerBo customerBo;
	//DI via Spring
	public void setCustomerBo(CustomerBo customerBo) {
		this.customerBo = customerBo;
	}

	public Object getModel() {
		return customer;
	}
	
	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

	//save customer
	public String addCustomer() throws Exception{
		
		//save it
		customer.setCreatedDate(new Date());
		customerBo.addCustomer(customer);
	 
		//reload the customer list
		customerList = null;
		customerList = customerBo.listCustomer();
		
		return "success";
	
	}
	
	//list all customers
	public String listCustomer() throws Exception{
		
		customerList = customerBo.listCustomer();
		
		return "success";
	
	}
	
}

6. Spring相關(guān)配置

幾乎所有的配置都是在這里完成是由Spring專門來整合。

CustomerBean.xml – 聲明 Spring 的 bean:Action, BO 和 DAO.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
 	<bean id="customerAction" class="com.yiibai.customer.action.CustomerAction">
		<property name="customerBo" ref="customerBo" />	
	</bean>

	<bean id="customerBo" class="com.yiibai.customer.bo.impl.CustomerBoImpl" >
		<property name="customerDAO" ref="customerDAO" />
	</bean>
	
   	<bean id="customerDAO" class="com.yiibai.customer.dao.impl.CustomerDAOImpl" >
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
 
</beans>

database.properties – 聲明數(shù)據(jù)庫詳細(xì)信息

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

DataSource.xml – 創(chuàng)建一個(gè)數(shù)據(jù)庫源的Bean 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
 <bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
     <value>WEB-INF/classes/config/database/properties/database.properties</value>
   </property>
</bean>
 
  <bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
  </bean>
 
</beans>

HibernateSessionFactory.xml – 創(chuàng)建一個(gè)SessionFactory Bean來集成Spring和Hibernate。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
<!-- Hibernate session factory -->
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
 
    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>
 
    <property name="mappingResources">
		<list>
          <value>com/yiibai/customer/hibernate/Customer.hbm.xml</value>
		</list>
    </property>	
 
</bean>
</beans>

SpringBeans.xml – 創(chuàng)建一個(gè)核心 Spring 的 bean 配置文件,作為中央的 bean 管理層。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<!-- Database Configuration -->
	<import resource="config/spring/DataSource.xml"/>
	<import resource="config/spring/HibernateSessionFactory.xml"/>
 
	<!-- Beans Declaration -->
	<import resource="com/yiibai/customer/spring/CustomerBean.xml"/>
 
</beans>

7. JSP 頁面

JSP頁面來顯示使用 Struts2 標(biāo)簽的元素。

customer.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 + Spring + Hibernate integration example</h1>

<h2>Add Customer</h2>
<s:form action="addCustomerAction" >
  <s:textfield name="name" label="Name" value="" />
  <s:textarea name="address" label="Address" value="" cols="50" rows="5" />
  <s:submit />
</s:form>

<h2>All Customers</h2>

<s:if test="customerList.size() > 0">
<table border="1px" cellpadding="8px">
	<tr>
		<th>Customer Id</th>
		<th>Name</th>
		<th>Address</th>
		<th>Created Date</th>
	</tr>
	<s:iterator value="customerList" status="userStatus">
		<tr>
			<td><s:property value="customerId" /></td>
			<td><s:property value="name" /></td>
			<td><s:property value="address" /></td>
			<td><s:date name="createdDate" format="dd/MM/yyyy" /></td>
		</tr>
	</s:iterator>
</table>
</s:if>
<br/>
<br/>

</body>
</html>

8. struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 	<constant name="struts.devMode" value="true" />
 	
	<package name="default" namespace="/" extends="struts-default">
		
		<action name="addCustomerAction" 
			class="customerAction" method="addCustomer" >
		    <result name="success">pages/customer.jsp</result>
		</action>
	
		<action name="listCustomerAction"
			class="customerAction" method="listCustomer" >
		    <result name="success">pages/customer.jsp</result>
		</action>
		
	</package>
	
</struts>

9. Struts 2 + Spring

要集成Struts2和Spring,只需注冊(cè)ContextLoaderListener監(jiān)聽器類,定義一個(gè)“contextConfigLocation”參數(shù)要求Spring容器來解析“SpringBeans.xml”,而不使用默認(rèn)的“applicationContext.xml”。

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>
  
  <filter>
	<filter-name>struts2</filter-name>
	<filter-class>
	  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	</filter-class>
  </filter>
  
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/classes/SpringBeans.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  
</web-app>

10. 運(yùn)行實(shí)例

在瀏覽器中打開網(wǎng)址 : http://localhost:8080/ssh/listCustomerAction.action

參考

  1. Struts2 + Hibernate集成實(shí)例
  2. Struts2 + Spring集成實(shí)例
  3. Struts2 + Hibernate 使用 Full Hibernate Plugin插件集成
代碼下載 - http://pan.baidu.com/s/1mgzt1Xm (含ssh相關(guān)類庫,詳見 lib 目錄,文件大小約:18M)。