首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

struts2 interceptor 完终日志功能

2012-08-22 
struts2 interceptor 完成日志功能第一步:interceptorsinterceptor nameauditInterceptor methodl

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);

}

}

热点排行