拦截器

拦截器interceptor
1、拦截器的配置
<interceptor name="拦截起名字" class="拦截器对应的JAVA类">
<param name="参数名">参数值</param>
</interceptor>
定义多个拦截器
<interceptors></interceptors>
2、拦截器栈的配置
<interceptor-stack name="拦截器站名">
<interceptor-ref name=""/>
<interceptor-ref name=""/>
....................
</interceptor-stack>
3、为拦截器栈指定参数分为两种情况
(1)定义拦截器时指定参数值:这种参数值为拦截器参数的默认值,通过<interceptor>元素来使用
<interceptor-stack name="拦截器1">
<interceptor-ref name="拦截器1">
<param name="参数1">参数值</param>
</interceptor-ref>
<interceptor-ref name="拦截器2"/>
</interceptor-stack>
给拦截器统一指定参数
<interceptor-stack name="拦截器1">
<param name="参数1">参数值1</param>
<param name="参数2">参数值2</param>
<interceptor-ref name="拦截器1"/>
<interceptor-ref name="拦截器2"/>
</interceptor-stack>
(2)使用拦截器时指定参数值:这种参数值是在使用该拦截器时动态分配的参数值,通过<interceptor-ref>元素来使用
4、使用拦截器
(拦截器和拦截器栈是用来拦截action的,拦截行为会在action的execute()方法被执行前执行)
struts.xml文件中的代码片段
<!--定义拦截器-->
<interceptors>
<interceptor name="拦截器1" class="对应的java类"/>
<interceptor name="拦截器2" class="对应的java类"/>
</interceptors>
<!--使用拦截器-->
<action name="public" class="yujinghuan.action.PublicAction">
<result name="success">/success.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="拦截器1"/>
<interceptor-ref name="拦截器2"/>
</action>
<action name="checkLogin" class="yujinghuan.action.DefaultAction"》
<result name="success">/success.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="拦截器1"/>
<interceptor-ref name="defaultStack"/><!--使用默认拦截器-->
</action>
5、配置默认拦截器
<struts>
<package name="testInterceptor" extends="struts-default">
<!--所有拦截器(栈)都定义在包下-->
<interceptors>
<interceptors .../>
<interceptor-stack..../>
</interceptors>
<default-interceptor-ref name="拦截器(栈)的名字"/><!--为此包配置默认拦截器-->
<action.../>
</package>
</struts>
6、为默认拦截器指定参数
7、自定义拦截器
Struts2提供了Interceptor接口,通过该接口可以很容易地实现一个拦截器类。开发者只需要直接或间接实现Inceptor接口
这个接口提供了3个方法
(1)init()由拦截器在执行前调用,主要用于初始化系统资源
(2)destroy()与init()方法对应,用于拦截器在执行之后销毁资源
(3)intercept()拦截器的核心方法,实现具体的拦截操作,返回一个字符串作为逻辑视图。
实例:用户登录拦截器
package yujinghuan.interceptor;//定义该类所在的包
import yujinghuan.action.*;//引入action包下的文件
import java.util.Map;//引入Map类
import com.opensymphony.xwork2.*;//引入xwork包下的文件
pubic class LoginInterceptor extends AbstractInterceptor{
//实现intercept()方法
public String intercept(ActionInvocation invocation) throws Exception{
//获取session()对象
Map session=invocation.getInvocationContext().getSession();
//获取session中的user,赋值给username属性
String username=(String)session.get("user");
//如果session中存在user,则进行后续操作,否则即没有user,表示用户还没有登录
if(username!=null&&username.length()>0){
return invocation.invoke();
}else{
//获取ActionContext对象
ActionContext ac=invocation.getInvocationContext();
//设置提示信息
ac.put("popedom","你还没有登录,请登录");
//返回Action对象中的LOGIN逻辑视图字符串
return Action.LOGIN;
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值