Java の Struts 2 インターセプターの使い方を教えてください

JavaでStruts2インターセプタを使用するには、次の手順に従います。

  1. インターセプターインターフェイスを実装するJavaクラスを作成する。なお、インターセプターインターフェイスはStruts2のインターセプターの基本インターフェイスである。例えば、CustomInterceptorという名前のクラスを作成してインターセプターインターフェイスを実装することができる。
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class CustomInterceptor implements Interceptor {

    @Override
    public void destroy() {
        // 在拦截器销毁时执行的代码
    }

    @Override
    public void init() {
        // 在拦截器初始化时执行的代码
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        // 在拦截器中执行的代码
        // 这里可以添加你需要的逻辑
        return invocation.invoke();
    }
}
  1. パッケージ
  2. インターセプター
<package name="default" extends="struts-default">
    <interceptors>
        <interceptor name="customInterceptor" class="com.example.CustomInterceptor"/>
    </interceptors>
    ...
</package>
  1. パッケージ
  2. ネイティブ日本語でこの文を言い換えてください。1つのオプションだけで結構です。<動作>
<package name="default" extends="struts-default">
    <interceptors>
        <interceptor name="customInterceptor" class="com.example.CustomInterceptor"/>
    </interceptors>
    <action name="exampleAction" class="com.example.ExampleAction">
        <interceptor-ref name="customInterceptor"/>
        <result>/example.jsp</result>
    </action>
    ...
</package>

この例では、exampleActionというアクションにcustomInterceptorというインターセプターが適用されます。

Struts2のインターセプターを使う基本的な流れは以上です。自身のインターセプタークラスでロジックを追加して、ご要望を満たしてください。

bannerAds