Java 在 Struts 2 拦截器中获取 HttpServletRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19241580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:21:49  来源:igfitidea点击:

Get HttpServletRequest in Struts 2 interceptor

javajspstruts2

提问by Alireza Fattahi

To get the HttpServletRequestin an interceptor I used below code:

为了获得HttpServletRequest拦截器,我使用了以下代码:

HttpServletRequest request =(HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);

I tried to implement ServletRequestAwarein the interceptor but it did not worked.

我试图ServletRequestAware在拦截器中实现,但没有奏效。

Are there any better ways to get HttpServletRequestin an Interceptor ?!

有没有更好的方法进入HttpServletRequest拦截器?!

采纳答案by Roman C

The servlet stuff you could get referencing servletConfiginterceptor. After this interceptor is invoked you could get servlet stuff from ServletActionContext.

您可以获得引用servletConfig拦截器的 servlet 内容。调用此拦截器后,您可以从ServletActionContext.

HttpServletRequest request = ServletActionContext.getRequest();

回答by Pankaj Sharma

you will get ActionInvoction try getInvocationContext()it will return instance of "ActionContext" try .get(HTTP_REQUEST);on this.

你会得到 ActionInvoction 尝试getInvocationContext()它会返回“ActionContext”的实例试试.get(HTTP_REQUEST);这个。

or

或者

use

ServletActionContext.getRequest()

回答by Ravi Thapliyal

You need to use ActionInvocation#getInvocationContext()to retrieve your request.

您需要使用ActionInvocation#getInvocationContext()来检索您的请求。

public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext context = invocation.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    // ...
}

回答by Siddheshwaar Patil

use

final HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                                               .get(ServletActionContext.HTTP_REQUEST);

it worked for me

它对我有用