java自定义拦截器用法实例
本文实例讲述了java自定义拦截器及其用法。分享给大家供大家参考。具体如下:
LoginInterceptor.java文件如下:
packagecom.tq365.util; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpSession; importorg.apache.struts2.ServletActionContext; importcom.opensymphony.xwork2.ActionInvocation; importcom.opensymphony.xwork2.interceptor.AbstractInterceptor; importcom.tq365.vo.User; /** *自定义的拦截器(放行登录操作和已经登录用户的操作) *@authorarchie2010 * */ publicclassLoginInterceptorextendsAbstractInterceptor{ privatestaticfinallongserialVersionUID=1406123004582563032L; @Override publicStringintercept(ActionInvocationinvocation)throwsException{ HttpServletRequestrequest=ServletActionContext.getRequest(); HttpSessionsession=ServletActionContext.getRequest().getSession(); Useruser=(User)session.getAttribute("USER"); //请求的url Stringpath=request.getServletPath(); booleanflag=false; if("/loginuser.jspx".equals(path)){ flag=true; }else{ if(user!=null){ flag=true; } } System.out.println(path); returnflag?invocation.invoke():"error"; } }
struts.xml文件如下:
<interceptors> <!--login拦截器--> <interceptorname="login"class="com.tq365.util.LongInterceptor"/> <interceptor-stackname="myInterceptor"> <interceptor-refname="login"/> <interceptor-refname="paramsPrepareParamsStack"/> </interceptor-stack> </interceptors> <default-interceptor-refname="myInterceptor"/>
希望本文所述对大家的java程序设计有所帮助。