spring+hibernate+struts 自己总结出的方法。

阅读更多

其实很简单,只需要两步:

1.在Web.xml文件中加入下面代码,从而加载spring的配置文件。

       
        contextConfigLocation    
        /WEB-INF/applicationContext.xml    
        
        
        context    
            
            org.springframework.web.context.ContextLoaderServlet    
            
        1    
       

 

2.为了使Action得到Bean,写一个BaseAction类,以得到想要的Bean。

 

BaseAction类

/**
 * 
 */
package com.leon.struts.action;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import com.leon.service.JobService;
import com.leon.service.NewsService;


/**
 * @author Leon Sui
 *
 * Date: Mar 4, 2009
 *
 */
public class BaseAction extends ActionSupport {
	protected Object getBean( String name ) {
		WebApplicationContext ctx = getWebApplicationContext();
		return ctx.getBean( name );
	}
	//得到Service bean
	protected JobService getJobService() {
		return (JobService)getBean("jobService");
	}
}

 其他的就是正常的注册bean,正常写struts-config配置文件。例如;

struts-config.xml





  
  

  

  
  
  
    
      
    

  

  

 


		
			
				com/leon/model/news.hbm.xml
				com/leon/model/job.hbm.xml
				com/leon/model/cases.hbm.xml
			
		
		
			
				 
					org.hibernate.dialect.MySQLDialect 
				true
				thread
				 
					org.hibernate.cache.EhCacheProvider 
				10
				update
			
		
		
			
		
	
	
	
		
			
		
	
	
		
			
		
	

 

Action类:红色的就是得到bean

/** 
 * MyEclipse Struts
 * Creation date: 03-03-2009
 * 
 * XDoclet definition:
 * @struts.action path="/job" name="jobForm" scope="request"
 * @struts.action-forward name="Job" path="/job.jsp"
 */
public class JobAction extends BaseAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ArrayList list = (ArrayList) getJobService().loadJobs();
		HttpSession session = request.getSession(true);
		session.setAttribute("job", list);
		return mapping.findForward("Job");
	}
}

  

一切正常使用。这是我用自己遇到的问题,加上8个小时的上网查找解决方法,最后总结出来的。希望大家能用得到!

你可能感兴趣的:(Struts,Hibernate,Spring,Bean,Servlet)