框架(Spring、Struts2和Hibernate三者)整合
總結一下就是:
1.Struts2與Spring相連的是:action不是Struts2框架new 出來的,而是從Spring的xml(applicationContext.xml)配置檔案中拿出
2.Spring和Hibernate相通的是:Hibernate的SessionFactory採用Spring注入,同時dao的實現類繼承Spring的類(HibernateDaoSupport)
3.其他的進入哪個領域就使用哪個框架
具體解釋見程式碼:
springStruts2Hibernate
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 載入Spring配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置字元過濾器,解決亂碼問題 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解決因session關閉而導致的延遲載入例外的問題 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- 配置Struts -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
伺服器啟動查詢Spring的配置檔案applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="regAction" class="cn.hncu.action.RegAction">
<property name="dao" ref="dao"></property>
</bean>
<bean id="dao" class="cn.hncu.dao.CustomerDaoJdbc" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定義事務傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/><!-- 除了上面定義的,其他的方法不走事務 -->
</tx:attributes>
</tx:advice>
<!-- 哪些類需要事務 -->
<aop:config>
<aop:pointcut id="alladdmethod"
expression="execution(* cn.hncu.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="alladdmethod" />
</aop:config>
</beans>
由Spring來new出來bean配置的類
第一個bean:RegAction.java
package cn.hncu.action;
import cn.hncu.dao.CustomerDao;
import cn.hncu.domain.Customer;
import com.opensymphony.xwork2.ActionSupport;
public class RegAction extends ActionSupport{
private String customerId;
private String name;
private String phone;
private CustomerDao dao;
public RegAction() {
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public CustomerDao getDao() {
return dao;
}
public void setDao(CustomerDao customerDao) {
this.dao = customerDao;
}
@Override
public String execute() throws Exception {
try {
Customer cus=new Customer();
cus.setCustomerId(this.getCustomerId());
cus.setName(this.getName());
cus.setPhone(this.phone);
this.getDao().addCustomer(cus);
return "success";
} catch (Exception e) {
return "input";
}
}
}
第二個bean:CustomerDao.java介面
package cn.hncu.dao;
import cn.hncu.domain.Customer;
public interface CustomerDao {
public abstract void addCustomer(Customer customer);
}
實現類CustomerDaoJdbc.java
package cn.hncu.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.hncu.domain.Customer;
public class CustomerDaoJdbc extends HibernateDaoSupport implements CustomerDao {
@Override
public void addCustomer(Customer customer) {
try {
System.out.println(customer);
// Session session=this.getSession();
// Transaction tr=session.beginTransaction();
// session.saveOrUpdate(customer);
// tr.commit();
// session.close();
this.getHibernateTemplate().save(customer);
} catch (DataAccessException e) {
e.printStackTrace();
}
}
}
Hibernate的配置檔案hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">hncu</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/mydb
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql1</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="cn/hncu/domain/Customer.hib.xml" />
</session-factory>
</hibernate-configuration>
值物件Customer.java
package cn.hncu.domain;
public class Customer {
private String customerId;
private String name;
private String phone;
public Customer() {
}
public Customer(String customerId) {
this.customerId = customerId;
}
public Customer(String customerId, String name, String phone) {
this.customerId = customerId;
this.name = name;
this.phone = phone;
}
public String getCustomerId() {
return this.customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", name=" + name + ", phone="
+ phone + "]";
}
}
值物件的對映檔案Customer.hib.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.hncu.domain.Customer" table="customers" catalog="mydb">
<id name="customerId" type="java.lang.String">
<column name="customerID" length="8"></column>
<generator class="assigned"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="40" />
</property>
<property name="phone" type="java.lang.String">
<column name="phone" length="16" />
</property>
</class>
</hibernate-mapping>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>註冊頁面</title>
</head>
<body>
<s:form action="regAction.action" method="post">
<s:textfield name="customerId" label="客戶編號"></s:textfield>
<s:textfield name="name" label="客戶姓名"/>
<s:textfield name="phone" label="客戶電話"></s:textfield>
<s:submit value="提交" align="center"></s:submit>
</s:form>
</body>
</html>
index的form表單請求regAction時請求,查詢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>
<package name="add" extends="struts-default">
<action name="regAction" class="cn.hncu.action.RegAction">
<result name="success">/jsps/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
成果展示:
主頁
提交成功:
同時儲存到資料庫中:
經測試,成功整合。
注:
注意:專案部署到web伺服器中可能報錯,因為Spring 2.5 AOP Libraries中的asm的三個jar包會和Hibernate 3.2 Core Libraries中的asm的jar包中的某些類中有衝突。所以一定要刪除Spring中的三個asm的jar包。
相關文章
- 【SSH框架】系列之 Spring 整合 Hibernate 框架框架Spring
- SSH框架查詢方法(struts2 Spring 3.1 Hibernate 4.1)框架Spring
- FLEX和spring、hibernate的整合FlexSpring
- spring整合struts2Spring
- [求助] STRUTS2和SPRING整合問題Spring
- Struts1、Struts2、Hibernate、Spring框架工作原理介紹Spring框架
- Spring 整合 HibernateSpring
- Struts2【與Spring整合】Spring
- spring整合struts2(續)Spring
- Struts2,Hibernate,Spring 環境配置Spring
- ssh框架搭建Struts2 06+spring2 5+hibernate3 2整合例項程式碼教程步驟框架Spring
- 深入淺出的理解框架(Struts2、Hibernate、Spring)與 MVC 設計模式框架SpringMVC設計模式
- sh_Spring整合HibernateSpring
- [摘]Struts+Spring+Hibernate整合Spring
- 關於Jdon框架和Hibernate的整合,Banq大哥請進框架
- Spring框架|整合JdbcTemplateSpring框架JDBC
- Spring整合其他框架Spring框架
- Spring整合Hibernate的事務管理Spring
- 在spring和hibernate框架下如何實現DDD思想?Spring框架
- 框架學習:webWork框架和struts2框架的結構和分析框架Web
- Spring與Hibernate整合中的session問題SpringSession
- spring json dwr struts2.0 hibernate整合SpringJSON
- Spring Boot整合熱部署框架Spring Boot熱部署框架
- 將Flex與Spring框架整合FlexSpring框架
- Spring框架(五)實戰Spring整合MybatisSpring框架MyBatis
- Hibernate--與Spring整合及增刪改查Spring
- struts+hibernate+spring 整合中出現的問題Spring
- Struts2+hibernate+spring配置程式整合下載Spring
- SSH三大框架的搭建整合(Spring+Hibernate+Struts2)實現增刪改查登入框架Spring
- Spring+SpringMVC+MyBatis框架整合SpringMVCMyBatis框架
- mybatis+spring+struts2框架整合MyBatisSpring框架
- spring boot 框架spring date jpa整合shiroSpring Boot框架
- 框架學習:hibernate框架的結構和分析框架
- Spring框架 - Spring和Spring框架組成Spring框架
- Struts2的框架框架
- Hibernate框架框架
- Spring4學習(四)Spring4中整合Hibernate4Spring
- 整合struts和hibernate開發嚮導 (轉)