java自定义拦截器用法实例
程序员文章站
2024-03-04 09:11:17
本文实例讲述了java自定义拦截器及其用法。分享给大家供大家参考。具体如下:
logininterceptor.java文件如下:
package com.tq...
本文实例讲述了java自定义拦截器及其用法。分享给大家供大家参考。具体如下:
logininterceptor.java文件如下:
package com.tq365.util; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpsession; import org.apache.struts2.servletactioncontext; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.interceptor.abstractinterceptor; import com.tq365.vo.user; /** * 自定义的拦截器(放行登录操作和已经登录用户的操作) * @author archie2010 * */ public class logininterceptor extends abstractinterceptor{ private static final long serialversionuid = 1406123004582563032l; @override public string intercept(actioninvocation invocation) throws exception { httpservletrequest request = servletactioncontext.getrequest(); httpsession session = servletactioncontext.getrequest().getsession(); user user = (user)session.getattribute("user"); // 请求的url string path = request.getservletpath(); boolean flag = false; if("/loginuser.jspx".equals(path)){ flag = true; }else{ if(user!=null){ flag = true; } } system.out.println(path); return flag ? invocation.invoke() : "error"; } }
struts.xml文件如下:
<interceptors> <!-- login拦截器 --> <interceptor name="login" class="com.tq365.util.longinterceptor"/> <interceptor-stack name="myinterceptor"> <interceptor-ref name="login"/> <interceptor-ref name="paramsprepareparamsstack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myinterceptor"/>
希望本文所述对大家的java程序设计有所帮助。