SSH简单整合

注意点:

  如果需要依赖注入,及通过get或者set方法来进行jsp页面数据到后台,需要缩小事务的范围。

理解:

 struts去执行浏览器和服务器之间的交互;

 spring完成对象的创建和对象的依赖关系以及事务的处理

 hibernate完成对数据的访问

疑难点:

1. 通过访问浏览器,使spring创建action,不需要自己创建

2. 将c3p0设置在spring中提高性能。

3. xml文件reference file contains error错误使得tomcat启动失败

    解决方案:Preferences >XML File> Validation > XML中"Honour all XML schema locations"前的对号去掉。它将禁用指向不同     schema位置相同命名空间引用的验证,仅以第一次找到的可验证的XML文件为结果。

    (来自:https://blog.csdn.net/u013087513/article/details/70991422)

web.xml配置:



	
	
		OpenSessionInView
		org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
	
	
		OpenSessionInView
		/*
	
	
	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	

	
	
		contextConfigLocation
		classpath:bean*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

公共bean.xml配置:





	
	
	
		
		
		
		
		
		
	

	
	
	
		
		
		
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				update
			
		
		
		
		
			
				classpath:SSH/*.hbm.xml
			
		
	
	
	
	
	
		
	
	
	
		
			
		
	
	
	
		
		
	
	
     

bean-dao.xml:

    
       
    

dao层代码:

package SSH;

import org.hibernate.SessionFactory;

public class AdminDao {
    private SessionFactory sf;
    public void setSf(SessionFactory sf) {
		this.sf = sf;
	}
    public Admin query(int id){
    	Admin admin = (Admin) sf.getCurrentSession().get(Admin.class, id);
    	return admin;
    }
}

Action代码:

package SSH;

import org.apache.struts2.ServletActionContext;

public class AdminAction {
   private AdminService service;
   public void setService(AdminService service) {
	this.service = service;
   }
   public String query(){
	   int id = 2;
	   Admin admin = service.query(id);
	   ServletActionContext.getRequest().setAttribute("admin", admin);
	   return "success";
   }
}

 

你可能感兴趣的:(SSH简单整合)