struts2关于action拦截器使用方法 现记录如下

struts2关于action拦截器使用方法  现记录如下 以便将来取用



struts2 与spring与hibernate整合


struts2中拦截action


业务要求:

后台输入

http://localhost:8080/mia/mia-admin/mia-login.jsp 为登入页面


  验证用户名和密码  正确则进入后台试图 ,但退出时 ,复制后台某个页面地址, 在浏览器输入回车 ,则转入登入页面

http://localhost:8080/mia/mia-admin/addarticleURL.action



现在使用

Struts2自定义拦截器

所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。该接口提供了三个方法:

1)      void init(); 在该拦截器被初始化之后,在该拦截器执行拦截之前,系统回调该方法。对于每个拦截器而言,此方法只执行一次。

2)      void destroy();该方法跟init()方法对应。在拦截器实例被销毁之前,系统将回调该方法。

3)      String intercept(ActionInvocation invocation) throws Exception; 该方法是用户需要实现的拦截动作。该方法会返回一个字符串作为逻辑视图。

除此之外,继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor是更简单的一种实现拦截器类的方式,因为此类提供了init()destroy()方法的空实现,这样我们只需要实现intercept方法。

<interceptor …>元素来定义拦截器

<interceptor-ref …>元素来使用拦截器。

使用自定义拦截器来完成用户权限的控制:当浏览者需要请求执行某个操作时,应用需要先检查浏览者是否登录,以及是否有足够的权限来执行该操作。

 AuthorityInterceptor.Java

package com.mia.util;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


@SuppressWarnings("rawtypes")
public class AuthorityInterceptor extends AbstractInterceptor {
  
	
	 private static final long serialVersionUID = 1358600090729208361L; 
	
    
	  
	 //拦截Action处理的拦截方法 
	 
	@Override
    public String intercept(ActionInvocation invocation) throws Exception { 
		
        // 取得请求相关的ActionContext实例 
		
        ActionContext context=invocation.getInvocationContext(); 
        
		Map session=context.getSession();
		
        //取出名为user的session属性 
		
		
       String user=(String)session.get("username"); 
       
       
       //如果没有登陆,或者登陆所有的用户名不是mia,都返回重新登陆 
       
       if(user!=null && user.equals("mia")){ 
    	   
    	   System.out.println("合法用户");
           return invocation.invoke();
           
       } else {
    	   //没有登陆,将服务器提示设置成一个HttpServletRequest属性 
           context.put("tip","您还没有登录,请登陆系统"); 
           
           return Action.LOGIN;
            
	}
      
   } 


}

由于我后台是一个用户 所以这里我限定死了 为mia

配置权限控制拦截器

struts.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="articleURL" extends="struts-default">
              
        <interceptors>
              <!-- 定义权限拦截器 -->
                  <interceptor name="authority" class="com.mia.util.AuthorityInterceptor"></interceptor>
                  
                  <!-- 定义一个包含权限权限拦截器 -->
                  <interceptor-stack name="mydefault">
                        <interceptor-ref name="defaultStack"/>
                         <interceptor-ref name="authority"/>
                  </interceptor-stack>
        </interceptors>
              
             <!-- 定义默认拦截器 -->
              <default-interceptor-ref name="mydefault"/>
              
              <!-- 定义全局处理结局 -->
              <global-results>
                   <result name="login">/mia-login.jsp</result>
              </global-results>
              
              
              
              
              <action name="addarticleURL" class="AddarticleURL"  method="publisharticleURL">
                 <result name="success" >/mia-admin/public/article/add.jsp</result>
                   
              </action>
    
    </package>
 

</struts>

一旦在某个包下定义了默认拦截器栈,在该包下的所有action都会使用此拦截器栈。对于那些不想使用些拦截器栈的action,则应该将它放置在其它的包下。


运行调试

在浏览器地址栏直接输入http://localhost:8080/mia/mia-admin/addarticleURL.action来访问,此动作配置了权限拦截器,所有被转到登录页面


登入后  就会登入到后台了



当然也可以不用配成拦截器栈

 <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="my" extends="struts-default">

       

        <interceptors>

        <!-- 定义权限控制拦截器 -->

        <interceptor name="authority" class=" com.mia.util.AuthorityInterceptor"/>

        </interceptors>

       

        <!-- 定义全局处理结果 -->

        <global-results>

        <!-- 逻辑名为login的结果,映射到/login.jsp页面 -->

        <result name="login">/mia-login.jsp</result>

        </global-results>

       

        <action name="addarticleURL" class="AddarticleURL"  method="publisharticleURL">
                 <result name="success" >/mia-admin/public/article/add.jsp</result>
              <!-- 使用拦截器 -->

            <interceptor-ref name="defaultStack"/>

            <interceptor-ref name="authority"/>
                   
     </action>

    

    </package>

</struts>



  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值