简体   繁体   English

在 Struts 2 拦截器中获取 HttpServletRequest

[英]Get HttpServletRequest in Struts 2 interceptor

To get the HttpServletRequest in an interceptor I used below code:为了在拦截器中获取HttpServletRequest ,我使用了以下代码:

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

I tried to implement ServletRequestAware in the interceptor but it did not worked.我试图在拦截器中实现ServletRequestAware但它没有奏效。

Are there any better ways to get HttpServletRequest in an Interceptor ?!有没有更好的方法在拦截器中获取HttpServletRequest ?!

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);
    // ...
}

The servlet stuff you could get referencing servletConfig interceptor.您可以通过引用servletConfig拦截器获得的 servlet 内容。 After this interceptor is invoked you could get servlet stuff from ServletActionContext .调用此拦截器后,您可以从ServletActionContext获取 servlet 内容。

HttpServletRequest request = ServletActionContext.getRequest();

use使用

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

it worked for me它对我有用

you will get ActionInvoction try getInvocationContext() it will return instance of "ActionContext" try .get(HTTP_REQUEST);你会得到 ActionInvoction try getInvocationContext()它会返回“ActionContext”的实例 try .get(HTTP_REQUEST); on this.在这一点上。

or

use使用

ServletActionContext.getRequest()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM