Struts2 自定义拦截器 二则
Struts2 自定义拦截器 二则
异常拦截器:
package org.roadway.lm.util;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author Huyvanpull
*
*/
@SuppressWarnings("serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
private String interceptorName;
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
this.logger.debug("进入" + this.getInterceptorName());
String result = null;
try
{
result = invocation.invoke();
}
catch (Exception exception)
{
this.logger.error(this.getExceptionInfo(exception));
throw exception;
}
return result;
}
private String getExceptionInfo(Exception exception)
{
StringBuffer bExceptionInfo = new StringBuffer();
bExceptionInfo.append(exception.toString());
bExceptionInfo.append("\n\t");
StackTraceElement[] stackTraceElements = exception.getStackTrace();
for (int i = 0; i < stackTraceElements.length; i++)
{
bExceptionInfo.append("[" + this.getInterceptorName() + "] "
+ stackTraceElements[i].toString() + "\n\t");
}
return bExceptionInfo.toString();
}
public String getInterceptorName()
{
return interceptorName;
}
public void setInterceptorName(String interceptorName)
{
this.interceptorName = interceptorName;
}
}
登陆检查拦截器:
package org.roadway.lm.util;
import java.util.Map;
import org.apache.log4j.Logger;
import org.roadway.lm.po.UserInfo;
import org.roadway.lm.user.action.UserInfoAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author tom_hui
*
*/
@SuppressWarnings("serial")
public class LoginCheckerInterceptor extends AbstractInterceptor
{
private String userSessionKey = "userInfo";
private String isCheckLogin = "true";
private Logger logger = Logger
.getLogger(LoginCheckerInterceptor.class);
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation actionInvocation) throws Exception
{
Object action = actionInvocation.getAction();
/** 如果设置拦截器不检查登陆 */
if ("false".equalsIgnoreCase(isCheckLogin))
{
actionInvocation.invoke();
}
/** 如果是登陆Action,放其通行 */
if (action instanceof UserInfoAction)
{
this.logger.info("登陆Action:" + UserInfoAction.class.getName());
return actionInvocation.invoke();
}
/** 从session中得到UserInfo的信息 */
Map session = actionInvocation.getInvocationContext().getSession();
UserInfo userInfo = (UserInfo) session.get(userSessionKey);
/** 如果Session中存在UserInfo对象 */
if (userInfo != null)
{
this.logger.info("用户" + userInfo.getUserName() + "("
+ userInfo.getUserId() + ")登陆了.");
return actionInvocation.invoke();
}
/** 如果没有登陆 */
else
{
return Action.LOGIN;
}
}
public String getUserSessionKey()
{
return userSessionKey;
}
public void setUserSessionKey(String userSessionKey)
{
this.userSessionKey = userSessionKey;
}
public String getIsCheckLogin()
{
return isCheckLogin;
}
public void setIsCheckLogin(String isCheckLogin)
{
this.isCheckLogin = isCheckLogin;
}
}
异常拦截器:
package org.roadway.lm.util;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author Huyvanpull
*
*/
@SuppressWarnings("serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
private String interceptorName;
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
this.logger.debug("进入" + this.getInterceptorName());
String result = null;
try
{
result = invocation.invoke();
}
catch (Exception exception)
{
this.logger.error(this.getExceptionInfo(exception));
throw exception;
}
return result;
}
private String getExceptionInfo(Exception exception)
{
StringBuffer bExceptionInfo = new StringBuffer();
bExceptionInfo.append(exception.toString());
bExceptionInfo.append("\n\t");
StackTraceElement[] stackTraceElements = exception.getStackTrace();
for (int i = 0; i < stackTraceElements.length; i++)
{
bExceptionInfo.append("[" + this.getInterceptorName() + "] "
+ stackTraceElements[i].toString() + "\n\t");
}
return bExceptionInfo.toString();
}
public String getInterceptorName()
{
return interceptorName;
}
public void setInterceptorName(String interceptorName)
{
this.interceptorName = interceptorName;
}
}
登陆检查拦截器:
package org.roadway.lm.util;
import java.util.Map;
import org.apache.log4j.Logger;
import org.roadway.lm.po.UserInfo;
import org.roadway.lm.user.action.UserInfoAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* @author tom_hui
*
*/
@SuppressWarnings("serial")
public class LoginCheckerInterceptor extends AbstractInterceptor
{
private String userSessionKey = "userInfo";
private String isCheckLogin = "true";
private Logger logger = Logger
.getLogger(LoginCheckerInterceptor.class);
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation actionInvocation) throws Exception
{
Object action = actionInvocation.getAction();
/** 如果设置拦截器不检查登陆 */
if ("false".equalsIgnoreCase(isCheckLogin))
{
actionInvocation.invoke();
}
/** 如果是登陆Action,放其通行 */
if (action instanceof UserInfoAction)
{
this.logger.info("登陆Action:" + UserInfoAction.class.getName());
return actionInvocation.invoke();
}
/** 从session中得到UserInfo的信息 */
Map session = actionInvocation.getInvocationContext().getSession();
UserInfo userInfo = (UserInfo) session.get(userSessionKey);
/** 如果Session中存在UserInfo对象 */
if (userInfo != null)
{
this.logger.info("用户" + userInfo.getUserName() + "("
+ userInfo.getUserId() + ")登陆了.");
return actionInvocation.invoke();
}
/** 如果没有登陆 */
else
{
return Action.LOGIN;
}
}
public String getUserSessionKey()
{
return userSessionKey;
}
public void setUserSessionKey(String userSessionKey)
{
this.userSessionKey = userSessionKey;
}
public String getIsCheckLogin()
{
return isCheckLogin;
}
public void setIsCheckLogin(String isCheckLogin)
{
this.isCheckLogin = isCheckLogin;
}
}