[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]
struts+spring开发整合时action中的servlet为null
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- laitaogood 2009-01-16 17:45关注
ServletContext servletContext = request.getSession().getServletContext();
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报