Struts2 请求为 null

发布于 2024-12-05 13:07:07 字数 1885 浏览 0 评论 0原文

我有一个非常奇怪的错误,当我尝试访问它时,我收到的请求为空。我总是使用相同的方法来获取它,但现在我遇到了这个错误。

我的操作如下所示:

package com.deveto.struts.actions;

import com.deveto.hibernate.mappings.Slider;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;


/**
 *
 * @author denis
 */
public class ContentAction extends ActionSupport implements ServletContextAware {

HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);

@Override
public String execute() throws Exception {

    System.out.println("request: " + request);

    return SUCCESS;
}

public ActionContext getAc() {
    return ac;
}

public void setAc(ActionContext ac) {
    this.ac = ac;
}

public HttpServletRequest getRequest() {
    return request;
}

public void setRequest(HttpServletRequest request) {
    this.request = request;
}

public HttpServletResponse getResponse() {
    return response;
}

public void setResponse(HttpServletResponse response) {
    this.response = response;
}

public ServletContext getSc() {
    return sc;
}

public void setSc(ServletContext sc) {
    this.sc = sc;
}

public void setServletContext(ServletContext sc) {
    this.sc = sc;
}
}

现在我什么也做不了,请求始终为空

 request: null

Very strange error I have, I am getting request as null when I try to access it. I always used the same method to get it, but now I am having this error.

My Action look like this:

package com.deveto.struts.actions;

import com.deveto.hibernate.mappings.Slider;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;


/**
 *
 * @author denis
 */
public class ContentAction extends ActionSupport implements ServletContextAware {

HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);

@Override
public String execute() throws Exception {

    System.out.println("request: " + request);

    return SUCCESS;
}

public ActionContext getAc() {
    return ac;
}

public void setAc(ActionContext ac) {
    this.ac = ac;
}

public HttpServletRequest getRequest() {
    return request;
}

public void setRequest(HttpServletRequest request) {
    this.request = request;
}

public HttpServletResponse getResponse() {
    return response;
}

public void setResponse(HttpServletResponse response) {
    this.response = response;
}

public ServletContext getSc() {
    return sc;
}

public void setSc(ServletContext sc) {
    this.sc = sc;
}

public void setServletContext(ServletContext sc) {
    this.sc = sc;
}
}

And now I can't do nothing, the request is always null

 request: null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只有一腔孤勇 2024-12-12 13:07:07

实现 ServletRequestAware 接口并在那里设置您的 request 变量,而不是在构造期间执行此操作。

但通常您不需要访问请求,因为 struts 的 params 拦截器会完成请求对象所需的所有工作。

ServletRequestAware 接口的文档中可以看出:

所有想要访问 servlet 请求对象的 Action 都必须实现此接口。

仅当 Action 在 servlet 环境中使用时,此接口才相关。

请注意,使用此接口会使 Action 绑定到 servlet 环境,因此应尽可能避免使用此接口,因为单元测试之类的事情会变得更加困难。

Implement the ServletRequestAware Interface and set your request variable there instead of doing that during construction.

But normally you don't need access to the request as the params interceptor of struts does all the work the request object is needed for.

From the documentation of the ServletRequestAware-Interface:

All Actions that want to have access to the servlet request object must implement this interface.

This interface is only relevant if the Action is used in a servlet environment.

Note that using this interface makes the Action tied to a servlet environment, so it should be avoided if possible since things like unit testing will become more difficult.

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