框架(Spring、Struts2和Hibernate三者)整合

总结一下就是:

1.Struts2与Spring相连的是:action不是Struts2框架new 出来的,而是从Spring的xml(applicationContext.xml)配置文件中拿出

2.Spring和Hibernate相通的是:Hibernate的SessionFactory采用Spring注入,同时dao的实现类继承Spring的类(HibernateDaoSupport)

3.其他的进入哪个领域就使用哪个框架

 

具体解释见代码:

springStruts2Hibernate

框架(Spring、Struts2和Hibernate三者)整合_第1张图片

 

web.xml



  	
  
  
  	contextConfigLocation
  	/WEB-INF/applicationContext.xml
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  
  	encodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  
  
  	encodingFilter
  	/*
  
  
	
		lazyLoadingFilter
		
			org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
		
	
  
  
  
  	struts
  	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
  	struts
  	/*
  
  
    index.jsp
  


服务器启动查找Spring的配置文件applicationContext.xml




	
		
	
	
		
	
	
	
		
		
	
	
	
	
		
	
	
	
		
			
			
		
	
	
	
		
		
	
 


由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






	hncu
	
		jdbc:mysql://127.0.0.1:3306/mydb
	
	
		org.hibernate.dialect.MySQLDialect
	
	mysql1
	1234
	
		com.mysql.jdbc.Driver
	
	




值对象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

  
 

	
	
		
			
			
		
		 
            
        
        
            
        
	


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>


  
    注册页面
  
  
  
   		
   			
   			
   			
   			
   		
  


index的form表单请求regAction时请求,查找struts.xml




	
		
			/jsps/success.jsp
			/index.jsp
		
	


成果展示:

主页

框架(Spring、Struts2和Hibernate三者)整合_第2张图片

 

提交成功:

框架(Spring、Struts2和Hibernate三者)整合_第3张图片

同时保存到数据库中:

框架(Spring、Struts2和Hibernate三者)整合_第4张图片

 

经测试,成功整合。

注:

注意:项目部署到web服务器中可能报错,因为Spring 2.5 AOP Libraries中的asm的三个jar包会和Hibernate 3.2 Core Libraries中的asmjar包中的某些类中有冲突。所以一定要删除Spring中的三个asmjar包。


你可能感兴趣的:(框架(SS2H,MyBatis),Java从入门到高级编程)