struts2 interceptor 完成日志功能
第一步:
<
interceptors>
<interceptor name="auditInterceptor" method="loginUser" >
<result name="success" type="redirectAction">
<param name="actionName">listWebUsersAction</param>
<param name="namespace">/</param>
</result>
<result name="input">pages/SignOn.jsp</result>
<result name="error">pages/signOnError.jsp</result>
<interceptor-ref name="auditInterceptorStack">
<param name="includeMethods">loginUser</param>
</interceptor-ref>
</action>
第二步:
public
class AuditInterceptor extends AbstractInterceptor{
private Date dateTime;
private String modifiedby;
private String action;
private String field;
private String input;
protected Logger log = Logger.getLogger(getClass());
public void init() {
}
@Override
public String intercept(ActionInvocation ai) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
Map session = ai.getInvocationContext().getSession();
Object action = ai.getAction();
String method = ai.getProxy().getMethod();
ai.invoke();
if (action instanceof UserLoginAction){
if(method.equals("loginUser")){
field = "Login";
this.action = "Login";
WebUsers webuser= (WebUsers)request.getSession().getAttribute(
"webUser");
input = "User:" + webuser.getUserName();
modifiedby= webuser.getUserName();
dateTime=new Date();
addSysLog();
}
}
return Action.SUCCESS;
}
private void addSysLog(){
WebAuditLogHome auditLogHome=
new WebAuditLogHome();
WebAuditLog auditlog=
new WebAuditLog(dateTime, modifiedby, this.action, field, input);
auditLogHome.attachDirty(auditlog);
}
}