<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用`@InterceptorRef`的 Struts2 自定義攔截器示例 > 原文: [https://howtodoinjava.com/struts2/struts-2-custom-interceptor-with-interceptorref-example/](https://howtodoinjava.com/struts2/struts-2-custom-interceptor-with-interceptorref-example/) 在以前的帖子中,我們了解了 [helloworld 應用](//howtodoinjava.com/struts-2/struts-2-hello-world-with-annotations/ "Struts2 hello world with annotations")和[設置 Struts2 應用](//howtodoinjava.com/struts-2/how-to-correctly-set-result-path-in-struts-2/ "How to correctly set result path in Struts2")的結果路徑。 現在,在這篇文章中,我將舉一個使用注解的自定義或用戶定義的攔截器配置示例。 攔截器是一個類,其每次訪問已配置的服務器資源時都會調用其預定義方法。 這可以在資源訪問之前或之后訪問。 請注意,此處的資源訪問也意味著 HTTP 請求處理。 ```java Steps summary: 1) Create custom interceptor class which implements com.opensymphony.xwork2.interceptor.Interceptor 2) Implement overridden methods 3) Give definition in struts.xml file 4) Apply on any Action class ``` **重要**:請注意,Struts2 隨附了許多現成的攔截器實現,因此請確保您確實需要創建自己的攔截器,或者有什么適合您的。 ## 1)& 2)創建自定義攔截器類并實現覆蓋的方法 以下是自定義攔截器的示例代碼,以及所有替代方法的實現。 請隨時以適當的方法添加應用邏輯。 ```java package com.howtodoinjava.struts2.example.web; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class DemoCustomInterceptor implements Interceptor { private static final long serialVersionUID = 1L; @Override public void destroy() { System.out.println("DemoCustomInterceptor destroy() is called..."); } @Override public void init() { System.out.println("DemoCustomInterceptor init() is called..."); } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("DemoCustomInterceptor intercept() is called..."); System.out.println(invocation.getAction().getClass().getName()); return invocation.invoke(); } } ``` ## 3)在`struts.xml`文件中給出定義 這是非常重要的一步,因為在這里您可以在 struts 上下文和運行時中注冊攔截器類。 ```java <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="demoCustomInterceptor" class="com.howtodoinjava.struts2.example.web.DemoCustomInterceptor" /> <interceptor-stack name="customStack"> <interceptor-ref name="demoCustomInterceptor"/> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> </package> <constant name="struts.convention.result.path" value="/WEB-INF/jsp/" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action," /> <constant name="struts.custom.i18n.resources" value="test" /> <constant name="struts.convention.default.parent.package" value="default"/> </struts> ``` ## 4)應用任何動作類 我在一個這樣的`TestAction`類中應用了上面的攔截器。 ```java package com.howtodoinjava.struts2.example.web; import java.util.Date; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @InterceptorRef(value="customStack") @Results({ @Result(name="success", location="success.jsp"), @Result(name="input", location="index.jsp") }) public class TestAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; private Date nowDate; @Override public void validate(){ if (name==null || name.length()==0) addActionError(getText("error.enter.message")); } @Actions({ @Action("/"), @Action("/test") }) @Override public String execute() throws Exception { nowDate = new Date(); return ActionSupport.SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getNowDate() { return nowDate; } } ``` 請注意,我已經使用`@InterceptorRef(value="customStack")`定義應用了攔截器。 它可以幫助您一次性應用多個攔截器。 如果僅只需要特定的攔截器,請使用其名稱,如下所示:`@InterceptorRef(value="demoCustomInterceptor")` ## 測試應用 當我運行上面的應用時。 我觀察到服務器在控制臺中的日志如下: ```java INFO: Loading global messages from [test] DemoCustomInterceptor init() is called... //some logs INFO: Server startup in 1146 ms DemoCustomInterceptor intercept() is called... com.howtodoinjava.struts2.example.web.TestAction ``` 請點擊下面的鏈接下載該項目的源代碼。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看