struts2-拦截器的基本使用
2019-03-29 14:52
295 查看
版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41083009/article/details/88892699
从struts2框架来看,拦截器几乎完成了Struts 2框架70%的工作,包括解析请求参数,将请求参数赋值给Action 属性,执行数据校验,文件上传等。Struts 2设计的灵巧性,更大程度地得益于拦截器设计,当需要扩展Struts 2功能时,只需提供对应拦截器,并将它配置在Struts2容器中即可:如果不需要该功能,只需取消该拦截器的配置即可。
接下来我们定义一个权限验证拦截器。
1、首先在登录成功后的界面做一个a标签;
2、修改Action实现类,将登录的用户名存到“userlll”中,后面用拦截器判断是否有权限进链接;
3、建立一个自定义拦截器;
package com.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; @SuppressWarnings("serial") public class UserInterceptor implements Interceptor{ public String intercept(ActionInvocation arg0) throws Exception { System.out.println("拦截器已启动"); String strName=(String) ActionContext.getContext().getSession().get("userlll"); if(strName.equals("1")) { String str = arg0.invoke(); System.out.println(str); System.out.println("拦截器已结束"); return str; } else { ActionContext.getContext().getSession().put("tip", "您没有权限查看"); return "output"; } } @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } }
从上面的代码看,拦截器主要是依据session中的username变量的值来判断用户是否是我规定有权限的用户(用户1)。
4、配置相应的struts.xml
执行我前面定义的标签,直接上配置,注意一下箭头的属性名,都是细节的东西。
5、结果
相关文章推荐
- struts2拦截器的使用(详解) 讲的不错,基本都包括了
- Struts2国际化、拦截器配置以及使用struts上传文件
- struts2自定义拦截器的实现及使用
- Struts2拦截器使用
- struts2之使用JSON插件实现Ajax(JSON基本知识)
- struts2中拦截器的使用(一)
- Struts2拦截器的使用
- 拦截器的基本使用方法
- struts2拦截器使用(用户session失效后的统一页面指向)
- Struts2 验证框架使用基本步骤
- 使用struts2拦截器,对session失效的校验
- 个人总结的Struts2拦截器使用和拦截栈的配置,基于注解的方式
- Struts2拦截器的使用
- 使用struts2拦截器实现防止表单数据重复提交
- Struts2之拦截器原理分析及使用-中
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- struts2 使用拦截器 实现用户权限的验证
- Struts2拦截器(Interceptor)的使用范例
- struts2拦截器的使用(二)