Customized Logging interceptor with bean value in Struts 2

761 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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"); 
0
On

I got the answer. This is the alternate way to retrieve request object

 HttpServletRequest request = ServletActionContext.getRequest();

But If you want to achieve the requestattribute which set into the action you need retrieve that after invocation. e.g.

 String result =  invocation.invoke();

after this

AuditBean auditBean2=(AuditBean) request.getAttribute("auditLogs");

then request object will not be null and you will get the value also.