struts1.1拦截器

来源:互联网 发布:十元夺宝源码 编辑:程序博客网 时间:2024/05/21 21:44
 

Usage Notes

To use SAIF, you must configure it as a Struts plugin, configure the interceptor config file, and write any interceptors you need.

Struts Plug-In Configuration

SAIF needs to be configured as a plugin in the Struts configuration file. In the plug-in configuration, the location of the interceptor configuration file needs to be defined. It should look something like this:

<plug-in className="net.sf.struts.saif.SAIFPlugin">
    <set-property property="interceptor-config" value="/WEB-INF/interceptor-config.xml" />
  </plug-in>     

Interceptor Configuration

All interceptors are defined in interceptor-config.xml (of course it could have any name). This file contains the interceptor definitions and how they should be applied. There are two ways to declare interceptors for Struts Actions: globally and by Action. When the Action is requested, first any global interceptors will be applied, then Action-specific interceptors.

The following interceptors are included in SAIF:

Included interceptors Class Description net.sf.struts.saif.ComponentInterceptor Performs inversion of control functionality. Sets any components the Action has defined it needs.

This is an example of an interceptor configuration file:

<interceptor-config>
<interceptor name="componentInterceptor" type="net.sf.struts.saif.ComponentInterceptor"/>
<interceptor name="testInterceptor" type="net.sf.struts.saif.TestInterceptor"/>

<default-interceptors>
<interceptor name="componentInterceptor"/>
</default-interceptors>

<action type="org.apache.struts.webapp.example.EditRegistrationAction">
<interceptor name="testInterceptor"/>
</action>
</interceptor-config>      

Interceptor Implementation

Interceptors can perform actions before and after a Struts Action is called. To write an interceptor, simple implement the net.sf.struts.saif.ActionInterceptor interface and implement the beforeAction() and afterAction() methods.

This is an example of an interceptor implementation:

public class TestInterceptor implements ActionInterceptor{public void beforeAction(Action action, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){log.debug("beforeAction called");}public void afterAction(Action action, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){log.debug("afterAction called");}private Log log = LogFactory.getLog(TestInterceptor.class);}          
原创粉丝点击