手机没电了 充电器没带 真痛苦
折磨代码吧
<interceptors>
<interceptor name="myInterceptor"
class="com.chiyu.action.admin.log.LoggerInterceptor">
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack">
</default-interceptor-ref>
package com.chiyu.action.admin.log;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.chiyu.model.Admin;
import com.chiyu.model.Adminoperationlog;
import com.chiyu.service.IAdminOperationLogService;
import com.chiyu.service.IAdminService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import freemarker.template.utility.StringUtil;
public class LoggerInterceptor extends AbstractInterceptor {
private IAdminOperationLogService adminOperationLogService;
private IAdminService adminService;
private static final long serialVersionUID = 1358600090729208361L;
@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("日志拦截器已经开始启动..");
String actionName = invocation.getAction().getClass().toString();
invocation.invoke();
final Map<String, Object> parameters = invocation
.getInvocationContext().getParameters();
boolean result = invocation.getProxy().getExecuteResult();
String method = invocation.getProxy().getMethod();
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = ServletActionContext.getRequest();
String ip = request.getRemoteAddr();
Map session = ctx.getSession();
// 取出名为user的session属性
String name = (String) session.get(Admin.ADMIN_KEY);
Admin admin = adminService.findAdminByAdminName(name);
// if (user != null) {
// System.out.println("操作的用户名为:" + user);
// System.out.println("操作者的IP为:" + ip);
// System.out.println("操作的时间为:"
// + new java.sql.Date(System.currentTimeMillis()));
// System.out.println("操作Action 的名称:" + actionName);
// System.out.println("操作的方法为:" + method);
// System.out.println("参数有:" + parameters);
// System.out.println("操作的方法结果为:" + result);
// }
System.out.println(admin);
if (admin != null) {
Adminoperationlog adminLog = new Adminoperationlog();
adminLog.setAdminId(admin.getAdminId());
adminLog.setAdminName(name);
adminLog.setDealTime(new java.sql.Date(System.currentTimeMillis()));
adminLog.setOperationIp(ip);
adminLog.setOperationSource(actionName + "---" + method + "---"
+ parameters + "---" + result);
adminOperationLogService.save(adminLog);
}
// if (parameters != null) {
// Set set = parameters.entrySet();
// Iterator iterator = null;
// if (set != null) {
// iterator = set.iterator();
// }
// while (iterator.hasNext()) {
// // iterator.next();
// Map.Entry entry = (Map.Entry) iterator.next();
// System.out.println(entry.getKey() + "----------"
// + entry.getValue());
// }
// }
return Action.SUCCESS;
}
public IAdminOperationLogService getAdminOperationLogService() {
return adminOperationLogService;
}
public void setAdminOperationLogService(
IAdminOperationLogService adminOperationLogService) {
this.adminOperationLogService = adminOperationLogService;
}
public IAdminService getAdminService() {
return adminService;
}
public void setAdminService(IAdminService adminService) {
this.adminService = adminService;
}
}