zoukankan      html  css  js  c++  java
  • struts2_11_实现自己的拦截器的定义

    1)登录界面代码:

    <%
    	//设置session的值keyword为user
    	request.getSession().setAttribute("user", "Enter");	
    %>
     	用户已登录

    2)退出界面的代码:

    <%
      //取消session中设定的值,即user为null	request.getSession().removeAttribute("user");
     %>
    	用户已经退出登录

    3)拦截器类PermissionInterceptor的代码:

    public class PermissionInterceptor implements Interceptor {
    
    	public void destroy() {// 销毁时调用
    	}
    
    	public void init() {// 启用时调用
    	}
    
    	// 该方法返回视图名称
    public String intercept(ActionInvocation invocation) throws Exception {
    
    		// 取得user的内容
    Object user = ActionContext.getContext().getSession().get("user");
    
    		// 假设user不为null表示用户已经登录,同意运行Action
    		if (user != null) {
    			return invocation.invoke();// 调用Action
    		}
    
      // 假设user为空,不调用Action,而且向session中存入字符串"你没有权		限运行该操作"
    	ActionContext.getContext().put("message", "你没有权限运行该操作");
    		return "success";
    	}
    }

    3)struts.xml文件的配置:

    <struts>
    	<package name="packageName" namespace="/test" 
      extends="struts-default">
    
    		<!--定义拦截器群 -->
    		<interceptors>
    			<interceptor name="permission" class="permissionInterceptor.PermissionInterceptor" />
    			<!-- 自己定义拦截器栈 -->
    			<interceptor-stack name="permissionStack">
    				<!--系统自己定义拦截器栈,先于自己定义拦截器引入 -->
    				<interceptor-ref name="defaultStack" />
    				<!--引入自己定义拦截器 -->
    				<interceptor-ref name="permission" />
    			</interceptor-stack>
    		</interceptors>
    
    		<!-- 定义默认全局拦截器 
      <default-interceptor-ref name="permissionStack" /> 
      -->
    
    		<!--定义全局视图 -->
    		<global-results>
    			<result name="success">/index.jsp</result>
    		</global-results>
    
    		<action name="hello*" 				
      class="interceptor.interceptorAction" method="{1}">
    			<!-- 手动调用系统拦截器,当使用手动调用时该拦截器时,定义的					系统默认拦截器不会使用 -->
    			<interceptor-ref name="permissionStack" />
    			<!-- 当调用自己定义拦截器时,仅仅有将系统拦截器,先调用 採用同
      时使用自己定义拦截器和系统拦截器 -->
    			<interceptor-ref name="interceptorName" />
    		</action>
    
    	</package>
    </struts>





    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Matplotlib 绘图库 基本使用方法
    linux socat命令
    linux shell重定向
    linux man命令
    bashttpd使用手册
    libcurl代码示例
    vim文件头部注释配置
    linux join命令
    iterm2切换显示屏vim乱行解决
    分形与混沌
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4917075.html
Copyright © 2011-2022 走看看