拦截器类
public class AdminInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L; //管理员操作相关的拦截器 @Override public String intercept(ActionInvocation invocation) throws Exception { String result = null; ActionContext actionContext = invocation.getInvocationContext(); if((actionContext.getSession().get("admin")==null||"".equals(actionContext.getSession().get("admin")))){ return "admin_login"; } result = invocation.invoke(); return result; } public static void main(String[] args) { // TODO Auto-generated method stub } }
admin.xml中应用
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="admin" extends="struts-default"> <interceptors> <interceptor name="AdminInterceptor" class="com.umgsai.txyl.interceptor.AdminInterceptor" /> <!-- 定义拦截器栈 --> <interceptor-stack name="myStack"><!-- 引用自定义拦截器进行身份验证 --> <interceptor-ref name="defaultStack" /> <interceptor-ref name="AdminInterceptor" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack" /><!--拦截器引用 --> <global-results><!-- 全局结果转发 --> <result name="admin_login">/admin/login.jsp</result><!-- 管理员身份验证失败 --> </global-results> <action name="image_upload" class="com.umgsai.txyl.action.UploadAction" method="site_modify"> <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param> <param name="savePath">/images</param> <result name="error">site_modify.jsp?site_id=${site_id}</result> <result name="success">site_modify.jsp?site_id=${site_id}</result> </action> <action name="admin_login" class="com.umgsai.txyl.action.AdminAction" method="execute"> <interceptor-ref name="defaultStack"/> <result name="admin_login_success">index.jsp</result><!-- //用户登录 --> <result name="admin_login_failed">login.jsp</result> </action> </package> </struts>