javastruts2customizationinterceptoractioncontext

Customized Logging interceptor with bean value in Struts 2


I am writing a customized interceptor for audit logs purpose. I want to get session attributes and request attributes in interceptor.

For example: I'll set Username into session and that I am getting too.

But the challenge is: I am defining one bean as audit bean where I am setting some values to bean

if (this.userName.equals("admin")) {
    user.setUserName(this.userName);
    sessionAttributes.put("USER", user);
    auditBean.setPerm("login success");
    requestAttributes.put("auditBean", auditBean);
    return "success";
} else {
    auditBean.setPerm("Login Failed initiaqlized");
    requestAttributes.put("auditBean", auditBean);
    addActionError(getText("error.login"));
    
    return "error";
}

this request attribute is ServletRequestAware obj. But this audit bean I am not able to retrieve into the interceptor, please help regarding this.

Map<String, Object> sessionAttributes = invocation.getInvocationContext().getSession();

achieving session like this.

Map<String, Object> requestAttributes = invocation.getInvocationContext().getParameters();

with above I am not able to retrieve request params. It's showing JSP request params, but not that I have set in action.


Solution

  • You can check that an invocation context in your interceptor is your action context, then get it directly from the context map.

    Map<String, Object> requestAttributes = (Map<String, Object>) invocation.getInvocationContext().get("request");
    

    or use alternate syntax

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