异常拦截器:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->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;
}
}
登陆检查拦截器:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->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;
}
}
分享到:
相关推荐
简单的struts2自定义拦截器 很适合web初学者
strut2 配置自定义拦截器 包括struts2.xml 和 action对应的xml 以及拦截器 可能有点简单 但是基本配置都有
struts2自定义拦截器.pdf
JSP 开发之Struts2内建自定义拦截器.docx
Struts2全局、局部自定义拦截器源码。
Struts2自定义拦截器实现防止重复提交
由于struts2标签的性能不好,项目组决定不使用,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现
NULL 博文链接:https://llxhna.iteye.com/blog/1067851
综合使用servlet filter与struts2 interceptor对权限进行验证。
Struts2_自定义拦截器 struts2_3500_my_interceptor
struts2的自定义拦截器代码
struts2 用拦截器 实现用户权限登录 可以直接运行,只单单用到struts的东西。
struts2的自定义拦截器代码例子
http://blog.csdn.net/zhiweiv/archive/2008/09/19/2954047.aspx
myeclipse平台下使用struts2.3框架搭建实例,理解struts2工作原理,了解struts2自定义类型转换,自定义拦截器,掌握struts2的mvc框架应用
自定义拦截器的实现以及拦截器的详细配置,配置文件里面有特别详细的注释,希望对刚接触拦截器的同学有帮助
通过struts2来自定义一个拦截器,需要定义struts2.xml和web.xml文件,详细代码见下载。
自定义的简单的struts,自己写一个和struts一样的xml文件,利用反射进行生成实例和注入参数,自定义拦截器和action