jzq1999 2009-01-16 13:27
浏览 405
已采纳

struts+spring开发整合时action中的servlet为null

[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]

  • 写回答

4条回答 默认 最新

  • laitaogood 2009-01-16 17:45
    关注

    ServletContext servletContext = request.getSession().getServletContext();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器