Domanda

I need to set a ServletRequest attribute within a Struts2 interceptor (not action class).

Does the ActionContext expose a Map<String, Object> to control request attributes, like it does for session attributes?

I see ActionContext implements a map. Is the ActionContext itself a wrapper for the request attributes?

È stato utile?

Soluzione 2

For code that is not inside an action class (RequestAware should be used for action classes), Struts2 can expose the servlet request attributes as a Map. They are accessible with:

Map request = (Map) ActionContext.getContext().get("request");

See Strus2 documentation for more details.

Altri suggerimenti

The ActionContext contains a request key that holds the request object. To answer you question: no the ActionContext is not a wrapper for request, however the request in Struts2 is a wrapper for the servlet request.

You can get the request from the action context like

HttpServletRequest request = ServletActionContext.getRequest();

That way is useful in interceptors, but in action better to implement ServletRequestAware

protected HttpServletRequest request;

public void setServletRequest(HttpServletRequest request) {
  this.request = request;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top