在Struts 2中使用bean值定制日志记录拦截器 [英] 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.
例如:我将 Username
设置为会话,而我也得到了.
For example: I'll set Username
into session and that I am getting too.
但是挑战是:我将一个bean定义为审计bean,在其中我将一些值设置为bean
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";
}
此请求属性是 ServletRequestAware
obj.但是此审计bean我无法检索到拦截器中,请对此提供帮助.
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();
实现这样的会话.
Map<String, Object> requestAttributes = invocation.getInvocationContext().getParameters();
上面的
我无法检索请求参数.它显示的是JSP请求参数,但不是我已经设置的.
with above I am not able to retrieve request params. It's showing JSP request params, but not that I have set in action.
推荐答案
您可以检查拦截器中的调用上下文是否是您的操作上下文,然后直接从上下文映射中获取它.
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");
或使用其他语法
Map requestAttributes = (Map) ActionContext.getContext().get("request");
这篇关于在Struts 2中使用bean值定制日志记录拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!