package com.framework.struts2.interceptor;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* @author wangbin
*
*/
@SuppressWarnings("serial")
public class LoggerInterceptor implements Interceptor{
private static final Logger logger = Logger.getLogger(LoggerInterceptor.class);
@Override
public void destroy() {
}
@Override
public void init() {
}
@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
logger.debug("Action:\t"+invocation.getAction().getClass().getName());
logger.debug("Params:");
//这个对象就是request.parameters
Map<String, Object> parameters = invocation.getInvocationContext().getParameters();
for (String key:parameters.keySet()){
String[] params = (String[]) parameters.get(key);
StringBuffer buffer = new StringBuffer();
for (String param:params){
buffer.append(","+param);
}
String paramValue = buffer.toString();
paramValue = paramValue.substring(1);
System.out.println(key+","+paramValue);
}
final String resultCode = invocation.invoke();
Result realResult = invocation.getResult();
if (realResult instanceof ServletDispatcherResult){
ServletDispatcherResult result = (ServletDispatcherResult)realResult;
logger.debug("jsp:\t"+result.getLastFinalLocation());
}
return resultCode;
}
}
以上是判断是否登陆和获取连接URL的拦截器。