struts2关于action拦截器使用方法 现记录如下
来源:互联网 发布:iphone故障检测软件 编辑:程序博客网 时间:2024/06/03 09:28
struts2关于action拦截器使用方法 现记录如下 以便将来取用
struts2 与spring与hibernate整合
struts2中拦截action
业务要求:
后台输入
http://localhost:8080/mia/mia-admin/mia-login.jsp 为登入页面
验证用户名和密码 正确则进入后台试图 ,但退出时 ,复制后台某个页面地址, 在浏览器输入回车 ,则转入登入页面
http://localhost:8080/mia/mia-admin/addarticleURL.action
Struts2自定义拦截器
所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。该接口提供了三个方法:
1) void init(); 在该拦截器被初始化之后,在该拦截器执行拦截之前,系统回调该方法。对于每个拦截器而言,此方法只执行一次。
2) void destroy();该方法跟init()方法对应。在拦截器实例被销毁之前,系统将回调该方法。
3) String intercept(ActionInvocation invocation) throws Exception;该方法是用户需要实现的拦截动作。该方法会返回一个字符串作为逻辑视图。
除此之外,继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor是更简单的一种实现拦截器类的方式,因为此类提供了init()和destroy()方法的空实现,这样我们只需要实现intercept方法。
<interceptor …>元素来定义拦截器
<interceptor-ref …>元素来使用拦截器。
使用自定义拦截器来完成用户权限的控制:当浏览者需要请求执行某个操作时,应用需要先检查浏览者是否登录,以及是否有足够的权限来执行该操作。
AuthorityInterceptor.Java
package com.mia.util;import java.util.Map;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;@SuppressWarnings("rawtypes")public class AuthorityInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1358600090729208361L; //拦截Action处理的拦截方法 @Override public String intercept(ActionInvocation invocation) throws Exception { // 取得请求相关的ActionContext实例 ActionContext context=invocation.getInvocationContext(); Map session=context.getSession(); //取出名为user的session属性 String user=(String)session.get("username"); //如果没有登陆,或者登陆所有的用户名不是mia,都返回重新登陆 if(user!=null && user.equals("mia")){ System.out.println("合法用户"); return invocation.invoke(); } else { //没有登陆,将服务器提示设置成一个HttpServletRequest属性 context.put("tip","您还没有登录,请登陆系统"); return Action.LOGIN; } } }
由于我后台是一个用户 所以这里我限定死了 为mia
配置权限控制拦截器
struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="articleURL" extends="struts-default"> <interceptors> <!-- 定义权限拦截器 --> <interceptor name="authority" class="com.mia.util.AuthorityInterceptor"></interceptor> <!-- 定义一个包含权限权限拦截器 --> <interceptor-stack name="mydefault"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="authority"/> </interceptor-stack> </interceptors> <!-- 定义默认拦截器 --> <default-interceptor-ref name="mydefault"/> <!-- 定义全局处理结局 --> <global-results> <result name="login">/mia-login.jsp</result> </global-results> <action name="addarticleURL" class="AddarticleURL" method="publisharticleURL"> <result name="success" >/mia-admin/public/article/add.jsp</result> </action> </package> </struts>
一旦在某个包下定义了默认拦截器栈,在该包下的所有action都会使用此拦截器栈。对于那些不想使用些拦截器栈的action,则应该将它放置在其它的包下。
运行调试
在浏览器地址栏直接输入http://localhost:8080/mia/mia-admin/addarticleURL.action来访问,此动作配置了权限拦截器,所有被转到登录页面登入后 就会登入到后台了
当然也可以不用配成拦截器栈
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="my" extends="struts-default"> <interceptors> <!-- 定义权限控制拦截器 --> <interceptor name="authority" class=" com.mia.util.AuthorityInterceptor"/> </interceptors> <!-- 定义全局处理结果 --> <global-results> <!-- 逻辑名为login的结果,映射到/login.jsp页面 --> <result name="login">/mia-login.jsp</result> </global-results> <action name="addarticleURL" class="AddarticleURL" method="publisharticleURL"> <result name="success" >/mia-admin/public/article/add.jsp</result> <!-- 使用拦截器 --> <interceptor-ref name="defaultStack"/> <interceptor-ref name="authority"/> </action> </package></struts>
- struts2关于action拦截器使用方法 现记录如下
- Struts2拦截器使用方法
- Struts2拦截器使用方法
- 【引用】Struts2拦截器使用方法
- 关于struts2拦截器
- struts2实现简单action时间拦截器
- struts2 拦截器拦截action中指定方法
- struts2拦截器的原理使用方法
- Struts2拦截器的一些使用方法
- 关于struts2拦截器错误
- 关于struts2中的拦截器
- 关于struts2的拦截器
- struts2拦截action多种方法
- Struts2中自定义拦截器导致Action注入参数丢失
- Struts2中Action标签与workflow拦截器的冲突。
- Struts2 中拦截器和Action的调用关系
- Struts2 In Action读书笔记二------------自定义开发拦截器
- Struts2 中拦截器和Action的调用关系
- UDP连接和TCP连接的异同
- ASP.Net中保护自定义的服务器控件
- ASP.NET中的doPostBack脚本函数实例
- C++函数重载
- SOAP简介
- struts2关于action拦截器使用方法 现记录如下
- C++指针问题与学习
- 三层架构之间的关系
- 中心湖
- JAVA游戏设计核心类(初学者适用)
- 推荐人工智能编程的必读书籍
- 6.087 Practical Programming in C, lec9
- c++中构造函数初始化的方法以及主要区别
- DataReader和DataSet的异同