在Struts1.x中使用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指向的页面,若不成功,
就会转向另一个页面。
分享到:
相关推荐
Struts1.x常用的开发包,需要学习struts1.x朋友可以下载
struts1.x和struts2.x区别
Struts 2.x权威指南(第3版)
freemarker与struts 1.x 结合,生成html 在http://blog.csdn.net/chen1255 的web中有详细描述关于freemarker与struts1.x的结合
在这里,通过一个小例子稍微总结了下struts1和struts2的区别,希望这个能帮助到初学struts的朋友
java Struts 1.x 框架 Web 开发 java Struts 1.x 框架 Web 开发
Struts1.x的登录示例,Struts1.x相关详细教程参考我的Blog:http://blog.csdn.net/boyazuo
struts1.x apps 所有最新的Struts1.x项目应用 struts1.x apps 所有最新的Struts1.x项目应用 欢迎大家下载。。。
适用于Struts1.x初学者(网摘共25节)
struts1.x入门教程
struts1.x多文件上传, 感觉不错.
struts1 国际化 源代码 struts1 国际化 工程
这是struts1.x标签! struts1.x是历经考验的成熟的框架! 此标签可以帮助你更好的学习struts1.x的标签!
Struts1.x-Jdbc,学习Struts1.的曾删改查
struts1.x 中文api 帮助文档
基于Struts1.x的简单电子商务系统,hiberntae实现数据的持久化,数据库是Mysql
struts1.x和mysql整合的登陆例子,包括用户的增删改查,涉及到LookupDispatchAction,的多个submit提交,静态验证,和读取数据库properties文件
解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证 你可以先参看我的blog文章:http://blog.csdn.net/qjyong/archive/2008/11/16/3311688.aspx 再决定是否下载。
struts1.x 学习总结 struts1.x 学习总结 struts1.x 学习总结 struts1.x 学习总结