`

在Struts1.x中使用net.sf.struts.saif.SAIFSpringPlugin配置Interceptor(拦截器)

阅读更多
  几个月前,Struts2发布,这个版本较struts1.x版本有了很大变化,其中一个就是增加了拦截器功能。这是个非常有用的功能,可是struts1.x却没有。其实,struts1.x可以配合插件,实现拦截器的功能。
       SAIF(Struts Action Invocation Framework)是一个开源组件,它让Struts框架具备Action拦截器与IOC的功能,这样你的1.x框架也就有了拦截器的功能。
      就是添加拦截器的步骤:
      1.将saif.jar包放入你的lib中。
      2.创建Interceptor类。比如我在这里创建一个类:
package  interceptor;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.sf.struts.saif.ActionHaveForwardInterceptor;

public class DisplayInterceptor implements ActionHaveForwardInterceptor ... {

     public ActionForward afterAction(Action arg0, ActionMapping arg1,
             ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)
             throws IOException, ServletException ... {
         // TODO Auto-generated method stub
         return null ;
     }

     public ActionForward beforeAction(Action action, ActionMapping mapping,
             ActionForm form, HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException ... {
         // TODO Auto-generated method stub
         System.out.println( " Inteceptor... " );
         if ( ! " fred " .equals(request.getParameter( " user_name " ))) ... {
             return mapping.findForward( " noPermission " );
         }
         return null ;
     }

}

3.写interceptor配置文件:interceptor-config.xml。这个配置文件中指定了interceptor类和要被拦截的action
<? xml version="1.0" encoding="UTF-8" ?>
< interceptor-config >
   < interceptor name ="displayInterceptor" type ="interceptor.DisplayInterceptor" />
    
   < action type ="/display" >
         < interceptor name ="displayInterceptor" />
   </ action >
    
</ interceptor-config >

4.在struts-config.xml中指定加载interceptor-config.xml
  < plug-in  className ="net.sf.struts.saif.SAIFSpringPlugin" >
     < set-property property ="interceptor-config" value ="/WEB-INF/interceptor-config.xml" />
   </ plug-in >

ok,配置完后,启动服务器,然后输入.../display.do?user_name=fred,回车,这时候,这个请求就会被拦截来,

进入beforeAction中,进行验证,若验证成功,return null,就会转到action的forward指向的页面,若不成功,

就会转向另一个页面。

http://hi.baidu.com/wmkoyo/blog/item/b4a65aa488570bf19152ee0c.html
分享到:
评论
11 楼 lin870521 2010-11-04  
<plug-in className="net.sf.struts.saif.SAIFPlugin">
    <set-property property="interceptor-config" value="/WEB-INF/interceptor-config.xml" />
  </plug-in>

和你的不一样


10 楼 lin870521 2010-11-04  
LZ 我按照你的方法进行了配置,但是没有拦截到Action?

package interceptor;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import net.sf.struts.saif.ActionInterceptor;

public class Struts1Interceptor implements ActionInterceptor {

public void afterAction(Action arg0, ActionMapping arg1, ActionForm arg2,
HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("come in afterAction...");
}

public void beforeAction(Action arg0, ActionMapping arg1, ActionForm arg2,
HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("come in beforeAction...");
}

}

配置文件和你的一样
帮忙看看,谢谢
9 楼 gqzyyxh 2010-09-11  
不能这样用!登录是一个action,退出又是一个action。拦截器只能对一个action进行拦截,也就是说beforeAction和afterAction针对的是一个action操作!
哦,那即是要另外定义一个
<interceptor name ="XX" type ="XXX" />
了,谢谢你~~
8 楼 gundumw100 2010-09-10  
gqzyyxh 写道
你好,还要麻烦你:
在自己定义的拦截器类中继承ActionHaveForwardInterceptor,里面有afterAction和beforeAction这两个方法,如果这两个方法我都写了代码,可是在拦截时有的只想用beforeAction拦截,有的只想用afterAction来拦截,这时候有什么办法解决么?
比如session,前面进入一些页面肯定要验证其是否已经登录,这是用到了beforeAction方法;之后推出登录时要使session失效,这是用到了afterAction方法,所以这两个方法里面都定义了。

不能这样用!登录是一个action,退出又是一个action。拦截器只能对一个action进行拦截,也就是说beforeAction和afterAction针对的是一个action操作!
7 楼 gqzyyxh 2010-09-10  
你好,还要麻烦你:
在自己定义的拦截器类中继承ActionHaveForwardInterceptor,里面有afterAction和beforeAction这两个方法,如果这两个方法我都写了代码,可是在拦截时有的只想用beforeAction拦截,有的只想用afterAction来拦截,这时候有什么办法解决么?
比如session,前面进入一些页面肯定要验证其是否已经登录,这是用到了beforeAction方法;之后推出登录时要使session失效,这是用到了afterAction方法,所以这两个方法里面都定义了。
6 楼 gqzyyxh 2010-09-10  
真的很感谢你,谢谢,现在终于成功了。原来type的值是自己拦截器的类名,谢谢~~
5 楼 gundumw100 2010-09-10  
type="interceptor.DisplayInterceptor"
gqzyyxh 写道
谢谢您,刚看邮箱jar包已经收到了。
还有个问题在interceptor-config.xml这个配置文件中
< interceptor nam="displayInterceptor"  
                           type="interceptor.DisplayInterceptor" />
这个type的值该填什么呢?
我试了interceptor.DisplayInterceptor和cn.softjob.util.DisplayInterceptor还有你的
com.tlt.app.interceptor.LoginInterceptor
页面都是报Failed to create interceptor of type xxx(type值)这种错误。

type="interceptor.DisplayInterceptor" 是你拦截器的类名啊,要确保路径正确!
4 楼 gqzyyxh 2010-09-10  
谢谢您,刚看邮箱jar包已经收到了。
还有个问题在interceptor-config.xml这个配置文件中
< interceptor nam="displayInterceptor"  
                           type="interceptor.DisplayInterceptor" />
这个type的值该填什么呢?
我试了interceptor.DisplayInterceptor和cn.softjob.util.DisplayInterceptor还有你的
com.tlt.app.interceptor.LoginInterceptor
页面都是报Failed to create interceptor of type xxx(type值)这种错误。
3 楼 gqzyyxh 2010-09-10  
不用麻烦了,自己部细心没看到,问题解决了、
2 楼 gqzyyxh 2010-09-09  
ActionHaveForwardInterceptor类和ActionInterceptor有区别么?
1 楼 gqzyyxh 2010-09-09  
你好,你能把saif.jar包发给我么,我邮箱是guoqiang-java@163.com谢谢了。
另外我用的是1:saif-0.1.jar2:saif-spring.jar3:spring.jar这3个jar包,可是自己创建interceptor类时不知道该继承那个类,我搜索了下DefaultInterceptor
和你写的ActionHaveForwardInterceptor类都没有,请问这是怎么回事呢,谢谢~~

相关推荐

Global site tag (gtag.js) - Google Analytics