Struts2 获取请求对象为 null
我在 Struts2 中遇到一个奇怪的错误,当尝试将属性设置到请求范围时,我收到一个空指针异常,
Struts Problem Report
Struts 检测到未处理的 例外:消息:文件: org/apache/catalina/connector/Request.java 行数:1,424
Stacktraces
java.lang.NullPointerException
org.apache.catalina.connector.Request.setAttribute(Request.java:1424)
org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:503)
javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:284)
com.inrev.bm.action.IRBrandMgmtAction.wrdTwts(IRBrandMgmtAction.java:81)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
下面给出了我的代码,
public class IRBrandMgmtAction extends ActionSupport implements SessionAware,ServletRequestAware {
private static final long serialVersionUID = 1L;
private Map session;
private HttpServletRequest request;
private IRWordToTrack wrdtotrack;
private IRBrandMgmtDAO brandMgmtDAO;
private static org.apache.log4j.Logger log = Logger.getLogger(IRBrandMgmtAction.class);
以及发生这种情况的方法,
public String wrdTwts()
{
ArrayList<IRBrandTrackBean> messages = null;
IRDateUtil dtUtil = new IRDateUtil();
String wordId = request.getParameter("wordId");
IRUser user = (IRUser) session.get("user");
IRWordToTrack word = brandMgmtDAO.getWord(Integer.parseInt(wordId));
if(word!=null)
{
messages = brandMgmtDAO.viewMsgForUser(word.getWord().toUpperCase().trim(), null, dtUtil.getTimeZoneOffset(user.getTimeZone()));
}
request.setAttribute("messages",messages);
return "tweets";
}
我使用的是 tomcat 6 服务器。
问候, 罗希特
I am facing a strange error in Struts2, I am getting a null pointer exception when trying to set attribute into request scope,
Struts Problem Report
Struts has detected an unhandled
exception: Messages: File:
org/apache/catalina/connector/Request.java
Line number: 1,424
Stacktraces
java.lang.NullPointerException
org.apache.catalina.connector.Request.setAttribute(Request.java:1424)
org.apache.catalina.connector.RequestFacade.setAttribute(RequestFacade.java:503)
javax.servlet.ServletRequestWrapper.setAttribute(ServletRequestWrapper.java:284)
com.inrev.bm.action.IRBrandMgmtAction.wrdTwts(IRBrandMgmtAction.java:81)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
My code is given below,
public class IRBrandMgmtAction extends ActionSupport implements SessionAware,ServletRequestAware {
private static final long serialVersionUID = 1L;
private Map session;
private HttpServletRequest request;
private IRWordToTrack wrdtotrack;
private IRBrandMgmtDAO brandMgmtDAO;
private static org.apache.log4j.Logger log = Logger.getLogger(IRBrandMgmtAction.class);
and the method where this is happening,
public String wrdTwts()
{
ArrayList<IRBrandTrackBean> messages = null;
IRDateUtil dtUtil = new IRDateUtil();
String wordId = request.getParameter("wordId");
IRUser user = (IRUser) session.get("user");
IRWordToTrack word = brandMgmtDAO.getWord(Integer.parseInt(wordId));
if(word!=null)
{
messages = brandMgmtDAO.viewMsgForUser(word.getWord().toUpperCase().trim(), null, dtUtil.getTimeZoneOffset(user.getTimeZone()));
}
request.setAttribute("messages",messages);
return "tweets";
}
I am using tomcat 6 server.
Regards,
Rohit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难知道问题的原因,但我想到了三个建议:
It's difficult to know the cause of your problem, but three suggestions come to mind:
将操作类设置为 RequestAware 而不是 ServletRequestAware 可能是一个想法。
在您当前的代码中,您确定正在调用您的 setServletRequest 方法吗?您是否定义了导致默认拦截器被丢弃的任何自定义拦截器?在会话/请求感知的情况下,我们对 ServletConfigInterceptor 感兴趣。
It might be an idea to make your action class RequestAware instead of ServletRequestAware.
In your current code are you sure that your setServletRequest method is being called? Have you defined any custom interceptors which cause the default interceptors to be discarded? In the case of Session/Request aware we're interested in the ServletConfigInterceptor.
使用,
ServletActionContext.getResponse();
ServletActionContext.getRequest();
获取请求和响应。
或者
首先调用此
use,
ServletActionContext.getResponse();
ServletActionContext.getRequest();
to get request and response.
OR
call this
<interceptor-ref name="defaultStack"/>
first