struts2 interceptor 完成日志功能

本文介绍了一种Struts2框架中自定义拦截器的具体配置方法,包括配置文件的详细内容及审计拦截器的实现代码。通过示例展示了如何为特定Action启用拦截器,并记录用户的登录行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:

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

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值