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

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

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

   <context-param>    
        <param-name>contextConfigLocation</param-name>    
        <param-value>/WEB-INF/applicationContext.xml</param-value>    
    </context-param>    
    <servlet>    
        <servlet-name>context</servlet-name>    
        <servlet-class>    
            org.springframework.web.context.ContextLoaderServlet    
        </servlet-class>    
        <load-on-startup>1</load-on-startup>    
     </servlet>  

 

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      path="/job"
      scope="request"
      type="com.leon.struts.action.JobAction"
      validate="false">
      <forward name="Job" path="/job.jsp" />
    </action>

  </action-mappings>

  <message-resources parameter="com.leon.struts.ApplicationResources" />

</struts-config>

 

<bean id="mySessionFactory" 
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="mappingResources">
			<list>
				<value>com/leon/model/news.hbm.xml</value>
				<value>com/leon/model/job.hbm.xml</value>
				<value>com/leon/model/cases.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect"> 
					org.hibernate.dialect.MySQLDialect </prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="current_session_context_class">thread</prop>
				<prop key="hibernate.cache.provider_class"> 
					org.hibernate.cache.EhCacheProvider </prop>
				<prop key="connection.pool_size">10</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>
	<!--Dao-->
	<bean id="newsDao" class="com.leon.dao.impl.NewsDaoImpl">
		<property name="sessionFactory">
			<ref bean="mySessionFactory"></ref>
		</property>
	</bean>
	<bean id="jobService" class="com.leon.service.impl.JobServiceImpl">
		<property name="jobDao">
			<ref bean="jobDao"></ref>
		</property>
	</bean>

 

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个小时的上网查找解决方法,最后总结出来的。希望大家能用得到!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值