针对框架中使用拦截器的功能的具体描述
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指向的页面,若不成功,
就会转向另一个页面。
分享到:
相关推荐
1.拦截器的使用示例 2.基于反射自己写的拦截器 3.在mvc框架配置即可使用
在struts2框架中主配置文件struts-defalut.xml 文件中定义了大量的拦截器和拦截器栈。通过 default-interceptor-ref元素定义了 当前应用的默认拦截器栈,对用户的每次请求都需要调用拦截器栈中的每个拦截器来拦截...
5、拦截器是包裹在过滤器中使用的。 复习 converter 转换器 i18n struts2 spring MVC 拦截器 interceptor 过滤器 filter web.xml implements filter filterchain arg2.doFilter(req,resp); 监听器 servlet...
三、添加拦截器对象,注入到容器的配置类中 另一种写法 四、最后application运行 我们对拦截器并不陌生,无论是 Struts 2 还是 Spring MVC 中都提供了拦截器功能,它可以根据 URL 对请求进行拦截,主要应用于登陆...
在现代的一些前端框架上,拦截器基本上是很基础但很重要的一环,比如Angular原生就支持拦截器配置,VUE的Axios模块也给我们提供了拦截器配置,那么拦截器到底是什么,它有什么用? 拦截器能帮助我们解决的 1、添加...
uni-router-interceptor 是一个基于uni-app框架的纯路由拦截插件 由于自己的项目需要一个类似 vue-router 的路由拦截功能,本人考察了 uni 插件市场的大部分这一类的插件,发现 很多插件都配置特别繁琐,并且编译成...
当需要扩展Struts2的功能时,我们只需要提供相应的拦截器并将它配置到Struts2容器中即可;当我们不需要使用的时候,只需要取消它在Struts2容器中的配置就行了。 1>配置拦截器主要使用四个配置元素: :用于配置...
Springboot-filter-listener-interceptor 过滤器、拦截器、监听器 Springboot-mongodb 与MongoDB 整合 Springboot-elasticsearch 与 Elasticsearch 整合 Springboot-i18n SpringBoot 国际化配置 SpringBoot-multi-...
6.interceptor 拦截器,将未登录的用户拦截至主页 7.service 服务层,将业务层操作与持久层操作绑定 8.test 单元测试 9.util 工具类,包括百度地图经纬度搜索,Json转String,md5加密,Python图片识别,mybatis...
整体使用springboot框架 项目启动类:Test1Application.java 实体层(model)entity:存放了页面需要传递数据对应字段的实体类,它和数据库中对应表字段的属性值保持一致,实现该类的set/...拦截器interceptor:实现登录拦
拦截器经配置后,可以把工作流程或者验证等常见功能作用到请求上。所有请求通过一组拦截器传送,之后再发送到Action类。Action类被执行后,请求按照相反顺序再次通过拦截器传送。 · 支持Ajax: 为了创建使用Ajax的...
但在调用之前,ActionInvocation会根据配置加载Action相关的所有Interceptor(拦截器)。 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果result。 Struts 2的核心控制器是...
该项目为可缓存的 Flume 拦截器提供了一个框架。 它是使用提供缓存服务的 Spring Framework 4.1.5 版实现的。 要实现自己的拦截器,请克隆此项目并实现 CacheableInterceptor 和 FlumeCacheService 的缺失部分。 ...
4.4.1 声明独立的拦截器和拦截器栈 74 4.4.2 将拦截器映射到动作组件 76 4.4.3 设置、覆盖拦截器参数 77 4.5 构建自定义拦截器 78 4.5.1 实现Interceptor接口 78 4.5.2 构建AuthenticationInterceptor拦截器 79 4.6 ...
interceptor 拦截器 template 模版 config 配置文件 cache 缓存文件 session Session文件 template_c 用于Smarty template_cache 用于Smarty log 日志 server 服务器日志 framework 框架日志 business 业务日志 ...
filter:Log4jFormatFilter(格式化log4j日志输出的),SessionCheckFilter(登陆拦截器) interceptor:权限拦截包 listener:目前只有session的监听器,用于监听session的 service:具体的业务处理包 util:项目工具包 ...
实现动态查询,使用了自定义注解,连接数据库表,为了保证数据的安全性,在SpringMVC中配置了自定义拦截器interceptor,过滤非法访问后台的请求类型。 因为主页会有大量的人员访问,所以为了降低服务器的压力,避免...
前言 SpringBoot_v2项目是努力打造springboot框架的极致细腻的脚手架。...│ ├─interceptor 拦截器 │ ├─log 日志记录AOP │ ├─domain 前台返回包 │ ├─quartz Spring定时器 │ └─support