struts2 的拦截器的应用

和webwork得差不多,首先写拦截器配置文件:xwork-losbgm-default.xml

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPExworkPUBLIC"-//OpenSymphonyGroup//XWork1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"
>
< xwork >
< include file ="struts-default.xml" />
< package name ="xwork-losbgm-default" extends ="struts-default" >
< interceptors >
< interceptor name ="employeeAccessInterceptor"
class
="tool.UserAccessInterceptor" >
</ interceptor >
< interceptor-stack name ="losbgmStack" >
< interceptor-ref name ="employeeAccessInterceptor" ></ interceptor-ref >
< interceptor-ref name ="defaultStack" />
</ interceptor-stack >
</ interceptors >
< default-interceptor-ref name ="losbgmStack" ></ default-interceptor-ref >
< global-results >
< result name ="error" > /error.jsp </ result >
< result name ="index" > /index.jsp </ result >
</ global-results >
</ package >
</ xwork >

在写一个拦截器类:UserAccessInterceptor.java

// Sourcefile:F:/java/Oil/src/com/xj/tools/UserAccessInterceptor.java

package tool;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserAccessInterceptor extends AbstractInterceptor ... {

@Override
publicvoiddestroy()...{
//TODOAuto-generatedmethodstub
super.destroy();
}


@Override
publicvoidinit()...{
//TODOAuto-generatedmethodstub
super.init();
}


@Override
publicStringintercept(ActionInvocationarg0)throwsException...{
//TODOAuto-generatedmethodstub

return"error";
}



}

再要实现拦截器的配置文件中先倒入文件再继承包就可以了:


<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >
< include file ="struts-default.xml" ></ include >
< include file ="xwork-losbgm-default.xml" ></ include >
< package name ="login" namespace ="/" extends ="xwork-losbgm-default" >
< action name ="login" class ="action.LoginAction" >
< result name ="success" > /index.jsp </ result >
</ action >
< action name ="main" >
< result > /main.jsp </ result >
</ action >
< action name ="Register" >
< result > /index.jsp </ result >
</ action >
< action name ="*" >
< result > /{1}.jsp </ result >
</ action >
</ package >
</ struts >

===================================================================================

struts2中的拦截器如何配置在action前后都执行呢?

public Stringintercept(ActionInvocationinvocation) ... {
//dosomejobbeforeinvocation
//...
Stringresult=invocation.invoke();
//dosomejobafterinvocation
//...
returnresult;
}

==============================================================================

你可能感兴趣的:(struts2)