`

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

2
1
分享到:
评论
2 楼 airdream 2009-03-12  
toeo 写道

# public class BaseAction extends ActionSupport {&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; protected Object getBean( String name ) {&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebApplicationContext ctx = getWebApplicationContext();&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ctx.getBean( name );&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; //得到Service bean&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; protected JobService getJobService() {&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (JobService)getBean("jobService");&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; # }&nbsp; 不是这么注入的吧??用srping生成sturts2的bean.这样方便管理.你这个都代码写的..使用get,set方法.

你说的是用spring代理struts的action吧,在spring的配置文件里写action bean然后注入service bean
1 楼 toeo 2009-03-05  
# 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"); 
#     } 
# } 

不是这么注入的吧??
用srping生成sturts2的bean.这样方便管理.你这个都代码写的..使用get,set方法.

相关推荐

Global site tag (gtag.js) - Google Analytics