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

 

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

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

Xml代码 复制代码
  1. <context-param>       
  2.      <param-name>contextConfigLocation</param-name>       
  3.      <param-value>/WEB-INF/applicationContext.xml</param-value>       
  4.  </context-param>       
  5.  <servlet>       
  6.      <servlet-name>context</servlet-name>       
  7.      <servlet-class>       
  8.          org.springframework.web.context.ContextLoaderServlet       
  9.      </servlet-class>       
  10.      <load-on-startup>1</load-on-startup>       
  11.   </servlet>    

 

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

 

BaseAction类

Java代码 复制代码
  1. /**  
  2.  *   
  3.  */  
  4. package com.leon.struts.action;   
  5. import org.springframework.web.context.WebApplicationContext;   
  6. import org.springframework.web.struts.ActionSupport;   
  7.   
  8. import com.leon.service.JobService;   
  9. import com.leon.service.NewsService;   
  10.   
  11.   
  12. /**  
  13.  * @author Leon Sui  
  14.  *  
  15.  * Date: Mar 4, 2009  
  16.  *  
  17.  */  
  18. public class BaseAction extends ActionSupport {   
  19.     protected Object getBean( String name ) {   
  20.         WebApplicationContext ctx = getWebApplicationContext();   
  21.         return ctx.getBean( name );   
  22.     }   
  23.     //得到Service bean   
  24.     protected JobService getJobService() {   
  25.         return (JobService)getBean("jobService");   
  26.     }   
  27. }  
/**
 * 
 */
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

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  3.   
  4. <struts-config>  
  5.   <data-sources />  
  6.   <form-beans >  
  7.   
  8.   </form-beans>  
  9.   
  10.   <global-exceptions />  
  11.   <global-forwards />  
  12.   <action-mappings >  
  13.     <action  
  14.       path="/job"  
  15.       scope="request"  
  16.       type="com.leon.struts.action.JobAction"  
  17.       validate="false">  
  18.       <forward name="Job" path="/job.jsp" />  
  19.     </action>  
  20.   
  21.   </action-mappings>  
  22.   
  23.   <message-resources parameter="com.leon.struts.ApplicationResources" />  
  24.   
  25. </struts-config>  

 

Xml代码 复制代码
  1. <bean id="mySessionFactory"    
  2.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  3.         <property name="mappingResources">  
  4.             <list>  
  5.                 <value>com/leon/model/news.hbm.xml</value>  
  6.                 <value>com/leon/model/job.hbm.xml</value>  
  7.                 <value>com/leon/model/cases.hbm.xml</value>  
  8.             </list>  
  9.         </property>  
  10.         <property name="hibernateProperties">  
  11.             <props>  
  12.                 <prop key="hibernate.dialect">    
  13.                     org.hibernate.dialect.MySQLDialect </prop>  
  14.                 <prop key="hibernate.show_sql">true</prop>  
  15.                 <prop key="current_session_context_class">thread</prop>  
  16.                 <prop key="hibernate.cache.provider_class">    
  17.                     org.hibernate.cache.EhCacheProvider </prop>  
  18.                 <prop key="connection.pool_size">10</prop>  
  19.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  20.             </props>  
  21.         </property>  
  22.         <property name="dataSource">  
  23.             <ref bean="dataSource"/>  
  24.         </property>  
  25.     </bean>  
  26.     <!--Dao-->  
  27.     <bean id="newsDao" class="com.leon.dao.impl.NewsDaoImpl">  
  28.         <property name="sessionFactory">  
  29.             <ref bean="mySessionFactory"></ref>  
  30.         </property>  
  31.     </bean>  
  32.     <bean id="jobService" class="com.leon.service.impl.JobServiceImpl">  
  33.         <property name="jobDao">  
  34.             <ref bean="jobDao"></ref>  
  35.         </property>  
  36.     </bean>  

 

Action类:红色的就是得到bean

Java代码 复制代码
  1. /**   
  2.  * MyEclipse Struts  
  3.  * Creation date: 03-03-2009  
  4.  *   
  5.  * XDoclet definition:  
  6.  * @struts.action path="/job" name="jobForm" scope="request"  
  7.  * @struts.action-forward name="Job" path="/job.jsp"  
  8.  */  
  9. public class JobAction extends BaseAction {   
  10.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  11.             HttpServletRequest request, HttpServletResponse response) {   
  12.         ArrayList list = (ArrayList) <SPAN style="COLOR: #ff0000"><STRONG>getJobService().</STRONG></SPAN>loadJobs();   
  13.         HttpSession session = request.getSession(true);   
  14.         session.setAttribute("job", list);   
  15.         return mapping.findForward("Job");   
  16.     }   
  17. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值