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;
publicclass
DisplayInterceptorimplements ActionHaveForwardInterceptor{
public ActionForward afterAction(Action arg0, ActionMapping arg1,
ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException{
// TODO Auto-generated method stub
returnnull;
}
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");
}
returnnull;
}
}
3.写interceptor配置文件:interceptor-config.xml。这个配置文件中指定了interceptor类和要被拦截的action
<?xml version="1.0" encoding="UTF-8"?>
<interceptor-config>
<interceptorname="displayInterceptor"
type="interceptor.DisplayInterceptor"/>
<actiontype="/display">
<interceptorname="displayInterceptor"/>
</action>
</interceptor-config>
4.在struts-config.xml中指定加载interceptor-config.xml
<plug-in
className="net.sf.struts.saif.SAIFSpringPlugin">
<set-propertyproperty="interceptor-config"
value="/WEB-INF/interceptor-config.xml"/>
</plug-in>
ok,配置完后,启动服务器,然后输入.../display.do?user_name=fred,回车,这时候,这个请求就会被拦截来,
进入beforeAction中,进行验证,若验证成功,return null,就会转到action的forward指向的页面,若不成功,
就会转向另一个页面。
分享到:
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
NULL 博文链接:https://cmexico.iteye.com/blog/621208
struts2拦截器应用小例子,与大家共分享
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
Struts2_自定义拦截器 struts2_3500_my_interceptor
struts2拦截器的使用说明文档,配置说明文档。可以看一下
struts1.2拦截器使用插件
传智播客struts2_3(拦截器_文件上传_输入验证_国际化).ppt
使用struts的拦截器拦截一些敏感性的词或一些非法或不文秘的言论
struts2(拦截器_文件上传_输入验证_国际化)
Struts2拦截器原理分析实例 博文链接:https://zmx.iteye.com/blog/457432
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
创建自己struts2拦截器,手工创建自己的拦截器实现
1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按...
完整可运行起来的SSH项目!中使用了SAIF 插件 实现拦截器的效果!并且有sql语句 !我使用的是mysql数据库,如果你使用别的数据库请改配置文件!运行成功后注意控制台的输出