[size=medium]目前的项目用到struts1.2+spring2.0进行整合开发,Action继承了struts的dispatchAction,然后在配置文件中使用了spring的代理机制对它进行管理,可是在Action中使用String dir=servlet.getServletContext().getRealPath("/StrengthFiles/honor"); 报空指针异常,输出 System.out.println(servlet); 打印也是null,如果不用spring来管理struts的action,则servlet输出有效值,这是怎么回事? 请遇到过同样问题的朋友帮忙解决,谢谢![/size]
[b]问题补充:[/b]
ServletContext application = getServletContext();
这一句在程序里还是报错,getServletContext()在程序里不支持,改成
ServletContext application = (ServletContext) getServlet();
后,输出:
Servlet.service() for servlet action threw exception
java.lang.IllegalArgumentException: ServletContext must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:54)
at com.abbcc.customer.TestAction.execute(TestAction.java:25)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
[b]问题补充:[/b]
[size=small]HttpServletContext还是不行 [/size]
ServletContext servletContext = request.getSession().getServletContext();
如果用到了spring,那么它有一个上下文,叫WebApplicationContext,你可以先初始化这个上下文,然后你就可以进行操作了
[code="java"]ServletContext application = getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext(application);[/code]
是HttpServletContext
用楼上的.