• Struts2拦截器的应用


     拦截器类

    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> 
  • 相关阅读:
    很好很強大..
    [草稿]初次接触RoR+MySQL之资源收集篇
    我的首页收藏链接之07年前的LIST
    生成不重复随机数
    The C# Programming Language(Third Edition) Part I
    The C# Programming Language(Third Edition) Part III
    The C# Programming Language(Third Edition) Part II
    求职技术题目收集② 算法
    求职路姊妹篇 笔试
    求职技术题目收集① 数据结构
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908124.html
Copyright © 2020-2023  润新知