当前位置: 代码迷 >> Web前端 >> struts2 interceptor 完终日志功能
  详细解决方案

struts2 interceptor 完终日志功能

热度:111   发布时间:2012-08-22 09:50:35.0
struts2 interceptor 完成日志功能
第一步:

<
interceptors>

<interceptor name="auditInterceptor" class="com.pulse.ipmanager.interceptor.AuditInterceptor"></interceptor>

<interceptor-stack name="auditInterceptorStack">

<interceptor-ref name="auditInterceptor"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

</interceptor-stack>

</interceptors>

。。。。。

<action name="loginAction"

class="com.pulse.ipmanager.action.UserLoginAction" 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);

}

}
  相关解决方案