大家好,
我用Struts+Spring +Hibernate 整和框架, 将 Struts 的 action 交由 Spring 管理了,
但是我在 Action中使用 this.getServlet().getServletConfig()获取servletConfig 却出现空指针异常,
怎么都想不通?
(
我使用jspsmartupload上传控件, 在Action 中初始化SmartUpload 对象
SmartUpload su = new SmartUpload();
su.initialize(this.getServlet().getServletConfig(), request, response);
)
[b]问题补充:[/b]
3位兄弟的回答都不是我要问的,
使用 jspsmartupload 上传,首先需要初始化smartupload对象,在jsp中使用,使用 :pageContext参数来初始化
[code="java"]
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
[/code]
但是,在action 中,只能使用它的另一个初始化方法:(action中无法获取pageContext)
[code="java"]
su.initialize(this.getServlet().getServletConfig(), request, response);
[/code]
现在,action交由 Spring 托管了,结果却无法使用 this.getServlet()来获取servlet了........
有什么方法可以解决啊???????
如果用到了spring,那么它有一个上下文,叫WebApplicationContext,你可以先初始化这个上下文,然后你就可以进行操作了
[code="java"] ServletContext application = getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext(application); [/code]
还是用request.getServletContext().getRealPath(); 吧.
getServlet() 这个返回是空;
struts 里面的action其实就是一个servelt 所以,要在struts 下面使用[color=red][b]smartupload 其使用方式根在servelt 下面所使用的方式一致.[/b]public [/color]class Action {
/**
* <p>The servlet to which we are attached.</p>
*/
protected ActionServlet servlet = null;
}
action 是一个类而struts的核心处理器是actionservelt如下:
public class ActionServlet extends HttpServlet {
// ----------------------------------------------------- Instance Variables
/**
* <p>Comma-separated list of context-relative path(s) to our configuration
* resource(s) for the default module.</p>
*/
protected String config = "/WEB-INF/struts-config.xml";
}
如果你用了spring ,因为action是一个类,spring 默认采用的是sun的代码即需要接口.所以你可以用上面人员所说的使用ServletContext application = getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext(application);
也可以使用spring 的对象代理机制