java自界說攔阻器用法實例。本站提示廣大學習愛好者:(java自界說攔阻器用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java自界說攔阻器用法實例正文
本文實例講述了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法式設計有所贊助。