Struts2 获取请求对象为 null

发布于 2024-09-16 16:54:23 字数 1909 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极致的悲 2024-09-23 16:54:23

很难知道问题的原因,但我想到了三个建议:

  • 也许是 Tomcat 错误,升级到最新版本。
  • 确保放入请求/会话上下文中的对象是小 bean,没有对其他对象的引用。
  • 你的代码有几种味道。在 Struts2 中,您(几乎)永远不应该访问 HttpRequest 对象(很少访问会话)。 Struts2 的要点是将您的操作与 Servlet 层(几乎)完全解耦;这可以简化测试并总体上让您满意。您的操作方法通常应该读取自己的字段(通常已经通过“参数拦截器”从 http 请求中设置),与服务层通信,设置一些其他自己的字段并返回结果。在 Struts2 方法中设置 httprequest 几乎总是一种不好的做法。

It's difficult to know the cause of your problem, but three suggestions come to mind:

  • Perhaps it's a Tomcat bug, upgrade to the most recent version.
  • Make sure the objects you put in Request/Session contexts are small beans, with no references to other objects.
  • Your code has several smells. In Struts2, you should (almost) never access the HttpRequest object (and rarely the session). The point of Struts2 is decoupling (almost) completely your action from the Servlet layer; this eases testing and in general keeps you happy. Your action method should normally read its own fields (typically already set from the http request by the "param interceptor"), talk to the service layer, set some other own fields and return a result. To set an httprequest inside a Struts2 method is almost always a bad practice.
与风相奔跑 2024-09-23 16:54:23

将操作类设置为 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.

放我走吧 2024-09-23 16:54:23

使用,
ServletActionContext.getResponse();
ServletActionContext.getRequest();

获取请求和响应。

或者

首先调用此

use,
ServletActionContext.getResponse();
ServletActionContext.getRequest();

to get request and response.

OR

call this <interceptor-ref name="defaultStack"/> first

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文