其实很简单,只需要两步:
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");
- }
- }
/**
*
*/
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) <SPAN style="COLOR: #ff0000"><STRONG>getJobService().</STRONG></SPAN>loadJobs();
- HttpSession session = request.getSession(true);
- session.setAttribute("job", list);
- return mapping.findForward("Job");
- }
- }