-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
struts2自定义拦截器
strust2框架中已经定义好了有许多,能解决WEB层的大部分问题,但有的时候还是不能解决一些特殊的需要,这个时候就需要开发人员自定义拦截器,具体的步骤为:
1.自定义一个类,实现Interceptor接口,重写接口中的方法,Interceptor接口中的方法如下:
public void destroy() {。。。。}
public void init() {。。。。}
public String intercept(ActionInvocation ai) t hrows Exception {
…………
returnnull;
}
intercept方法中接收的参数ActionInvocation对Action的信息进行了封装,通过此类可以获取Action相关的对象,从而可以处理用户请求。
从Interceptor接口中声明的方法中可以看出来,其实就是一个拦截器的生命周期相关的方法,与过滤器的设计思想类似,开发人员需要在intercept(……)方法中写处理请求的代码。
2.自定义的拦截器需要声明及配置才能被构架调用:
***************************************************************
<package name=" " namespace="/ "extends="struts-default">
<interceptors>
<!—第一步:声明一个拦截器 -->
<interceptor name="UserDefinedInerceptor" class="com.struts2.aop.UserDefinedInerceptor">
</interceptor>
<!—第二步:定义一个拦截器站(含自定义的拦截器) -->
<interceptor-stack name="UserDefined-stack">
<interceptor-ref name="UserDefinedInerceptor" />
</interceptor-stack>
</interceptors>
<!—第三步:设置默认执行的拦截器站为自定义的 -->
<default-interceptor-ref name="UserDefined-stack" />
<action name=" " class=" "method=" ">
结果类型内容(略)
</action>
</package>
--------------------------------------------------------2015年8月6日笔记----------------------------------------------------------------------------------