传智Struts2笔记(11)自定义拦截器
自定义拦截器
要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口:
public class PermissionInterceptor implements Interceptor { private static final long serialVersionUID = -5178310397732210602L; public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("进入拦截器");if(session里存在用户){String result = invocation.invoke();}else{return “logon”;}//System.out.println("返回值:"+ result);//return result; }}<package name="itcast" namespace="/test" extends="struts-default"><interceptors> <interceptor name=“permission" /> <interceptor-stack name="permissionStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name=" permission " /> </interceptor-stack> </interceptors><action name="helloworld_*" method="{1}"><result name="success">/WEB-INF/page/hello.jsp</result><interceptor-ref name="permissionStack"/></action></package>