论坛首页 入门技术论坛

webwork怎么获取spring的ApplicationContext

浏览 5980 次
该帖已经被评为新手帖
作者 正文
   发表时间:2006-11-19  
在struts,可以在BaseAction里获得。如spring自带的jpetstore的例子:
package org.springframework.samples.jpetstore.web.struts;

import javax.servlet.ServletContext;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;

import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Superclass for Struts actions in JPetStore's web tier.
 * 
 * <p>
 * Looks up the Spring WebApplicationContext via the ServletContext and obtains
 * the PetStoreFacade implementation from it, making it available to subclasses
 * via a protected getter method.
 * 
 * <p>
 * As alternative to such a base class, consider using Spring's ActionSupport
 * class for Struts, which pre-implements WebApplicationContext lookup in a
 * generic fashion.
 * 
 * @author Juergen Hoeller
 * @since 30.11.2003
 * @see #getPetStore
 * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
 * @see org.springframework.web.struts.ActionSupport
 */
public abstract class BaseAction extends Action {

	private PetStoreFacade petStore;

	public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		if (actionServlet != null) {
			ServletContext servletContext = actionServlet.getServletContext();
			WebApplicationContext wac = WebApplicationContextUtils
					.getRequiredWebApplicationContext(servletContext);
			this.petStore = (PetStoreFacade) wac.getBean("petStore");
		}
	}

	protected PetStoreFacade getPetStore() {
		return petStore;
	}

}


这样在每个派生于BaseAction的struts action类都可以使用super.getPetStore.method()来调用petStore bean中的方法,而petStore bean又是整个应用的一个facade,这样非常方便。

在webwork中怎么样达到差不多的功能呢?
   发表时间:2006-11-19  
依葫芦画瓢,先做个例子试试啊!
我没用过webwork,但是和struts应该是差不多的;你也可以把配置文件放在web.xml中加载啊,那样不是更方便吗?
0 请登录后投票
   发表时间:2006-11-19  
WebApplicationContextUtils.getWebApplicationContext(ServletContext context);
0 请登录后投票
   发表时间:2006-11-19  
同问,如果是后台线程,如何得到ServletContext,用ServletActionContext.getServletContext()得到的是NULL
0 请登录后投票
   发表时间:2006-11-26  
是对原来项目的一次改版,原来用struts + JDBC做的,准备用webwork重写,最后放弃了。现在改用spring + struts + iBATIS + JSTL来实现。这样只改前台视图,后台就不动了。省了不少工作量。呵呵。
0 请登录后投票
   发表时间:2006-11-27  
引用
同问,如果是后台线程,如何得到ServletContext,用ServletActionContext.getServletContext()得到的是NULL


可以弄个全局Servlet来共享.
0 请登录后投票
   发表时间:2006-11-27  
两法:
1是直接从获取WebApplicationContext对象,如放到自己的WebAppContext static下
2能过加载servlet(优先级为1),或listener来设置
实际上第二种方法与spring本身提供的方法差不多,它实现就是把你的WebApplicationContext对象放入它的servletContext的一个attribute 中,然后再通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
读取.

两种方法俺都试过,距离差异在俺做的基础中还没怎么体现...
0 请登录后投票
   发表时间:2006-12-05  
我猜你的想法是想用application.xml里面定义的service类吧,如果是的话,你可以查看下webwork文档中提供的关于与spring继承的文章,可以做到在webwork的action中直接注入spring的bean
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics