代码:
package com.web.action;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class HistoryLogStack implements Interceptor {
/**
*
*/
private static final long serialVersionUID = -1385902554116735266L;
public void destroy() {
System.out.println("日志拦截器销毁...");
}
public void init() {
System.out.println("日志拦截器初始化...");
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("日志拦截器已经开始启动..");
String result = invocation.invoke();
String actionName= invocation.getAction().getClass().toString();
String method = invocation.getProxy().getMethod();
final Map <String,Object> parameters = invocation.getInvocationContext().getParameters();
System.out.println("Action: " + actionName + ", Method: " + method);
System.out.println("拦截的参数为:");
for (Map.Entry<String, Object> map : parameters.entrySet()) {
System.out.println(map.getKey() + "--" + map.getValue());
}
if(method != null && method.startsWith("save")){
System.out.println("Execute save method..." + new Date());
}
return result;
}
}
配置:
<interceptors>
<interceptor name="historyLog" class="com.web.action.HistoryLogStack" />
<interceptor-stack name="myDefStack">
<interceptor-ref name="historyLog" />
<interceptor-ref name="paramsPrepareParamsStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefStack" />