💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 自定义拦截器 MyIntercept1: ~~~ package com.like.intercept; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyIntercept1 implements Interceptor { @Override public void destroy() {} @Override public void init() {} @Override public String intercept(ActionInvocation actionInvocation) throws Exception { System.out.println("intercept1 before"); //放行 String invoke = actionInvocation.invoke(); System.out.println("intercept1 after"); return invoke; //返回一个别名 } } ~~~ MyIntercept2: ~~~ package com.like.intercept; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyIntercept2 implements Interceptor { @Override public void destroy() {} @Override public void init() {} @Override public String intercept(ActionInvocation actionInvocation) throws Exception { System.out.println("intercept2 before"); //放行 String invoke = actionInvocation.invoke(); System.out.println("intercept2 after"); return invoke; } } ~~~ struts2.xml: ~~~ <package name="default" namespace="/" extends="struts-default"> <!--为package下面所有action申明拦截器--> <interceptors> <!--申明拦截器--> <interceptor name="MyIntercept1" class="com.like.intercept.MyIntercept1"/> <interceptor name="MyIntercept2" class="com.like.intercept.MyIntercept2"/> <!--申明拦截器组--> <interceptor-stack name="MyIntercepts"> <!--调用默认组.不调用会导致struts2原有功能无法使用--> <interceptor-ref name="defaultStack"/> <!--拦截器组所使用的拦截器名--> <interceptor-ref name="MyIntercept1"/> <interceptor-ref name="MyIntercept2"/> </interceptor-stack> </interceptors> <!--执行在访问action的时候需要执行的拦截器组--> <default-interceptor-ref name="MyIntercepts"/> <action name="index" class="com.like.HelloAction" method="index"> <!--为单个action指定拦截器.可以指定单个拦截器,也可以指定拦截器组--> <!--如果指定了单个拦截器,那么为package申明全局的所有拦截器(包括默认)将不作用于此--> <interceptor-ref name="MyIntercept1"/> <result name="success" type="dispatcher">/index.jsp</result> </action> </package> ~~~