docker 菜鳥,菜鳥學Struts2——Interceptors

 2023-11-19 阅读 21 评论 0

摘要:  昨天學習Struts2的Convention plugin,今天利用Convention plugin進行Interceptor學習,雖然是使用Convention plugin進行零配置開發,這只是在Interceptor的使用上,定義Interceptor還是使用strutx.xml配置形式。Interceptors模塊如下圖:

  昨天學習Struts2的Convention plugin,今天利用Convention plugin進行Interceptor學習,雖然是使用Convention plugin進行零配置開發,這只是在Interceptor的使用上,定義Interceptor還是使用strutx.xml配置形式。Interceptors模塊如下圖:?

(1)編寫Interceptor

自定義Interceptor可以實現com.opensymphony.xwork2.interceptor.Interceptor接口,也是繼承com.opensymphony.xwork2.interceptor.AbstractInterceptor,實際上AbstractInterceptor就是實現了Interceptor接口,并提供init()和destroy()的默認實現(空實現)。這里直接繼承AbstractInterceptor,寫一個為Action設值的攔截器,代碼如下:?

 1 package yaolin.core.interceptor;
 2 import com.opensymphony.xwork2.ActionInvocation;
 3 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 4 import yaolin.core.action.OutAction;
 5 
 6 public class ValueInterceptor extends AbstractInterceptor {
 7     private static final long serialVersionUID = -8801972415271737380L;
 8 
 9     //resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
10     @Override
11     public String intercept(ActionInvocation invocation) throws Exception {
12         OutAction action = null;
13         if (invocation.getAction() instanceof OutAction) {
14             action = (OutAction) invocation.getAction();
15             action.setBefore("before");
16         }
17         invocation.invoke();
18             // invoke all interceptor
19             // resultCode = invokeActionOnly();
20             // executeResult(); 這里已經執行了executeResult(),所以下面的return不會對result產生影響。
21         // continue valueInterceptor
22         if (action != null) {
23             // 上面已經執行了invokeActionOnly(),所以這里的設值不會影響值棧
24             action.setAfter("after");
25         }
26         return invocation.getResultCode();
27     }
28 
29 }

docker 菜鳥??(2)配置Interceptor

在struts.xml配置這個Interceptor :

 1 <!DOCTYPE struts PUBLIC
 2     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 3     "http://struts.apache.org/dtds/struts-2.3.dtd">
 4 <struts>
 5     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 6     <package name="v">
 7         <interceptors>
 8             <interceptor name="valueInterceptor" class="yaolin.core.interceptor.ValueInterceptor"></interceptor>
 9         </interceptors>
10     </package>
11 </struts>

(3)在Action中使用Interceptor

Action除了execute()方法外有兩個屬性before和after,并提供getter和setter,在Action中加入@InterceptorRef("valueInterceptor")注解,指定攔截器"valueInterceptor"

 1 package yaolin.core.action;
 2 
 3 import org.apache.struts2.convention.annotation.InterceptorRef;
 4 import org.apache.struts2.convention.annotation.ParentPackage;
 5 
 6 /**
 7  * /out.action
 8  * @author yaolin
 9  */
10 @ParentPackage("v")
11 @InterceptorRef("valueInterceptor")
12 public class OutAction {
13 
14     private String before;
15     private String after;
16     
17     public String execute() {
18         return "success";
19     }
20     
21     public String getBefore() {
22         return before;
23     }
24     public void setBefore(String before) {
25         this.before = before;
26     }
27     public String getAfter() {
28         return after;
29     }
30     public void setAfter(String after) {
31         this.after = after;
32     }
33 }

?注意這里要加入@ParentPackage("v")指定繼承的父包,如果這里不指定的話會出現找不到Interceptor的異常。這一段在文檔中是這樣描述的:

大學菜鳥驛站放假嗎。

包配置(com.opensymphony.xwork2.config.entities.PackageConfig)

頁面上直接獲取before和after的值,out.jsp如下:

?

結果(解釋見Interceptor的注釋):

?

下面是使用xml配置Action時,攔截器的配置例子:

 1 <package name="default" extends="struts-default">
 2    <interceptors>
 3         <interceptor name="timer" class=".."/>
 4         <interceptor name="logger" class=".."/>
 5         <interceptor-stack name="myStack">
 6            <interceptor-ref name="timer"/>
 7            <interceptor-ref name="logger"/>
 8         </interceptor-stack>
 9     </interceptors>
10  
11 <action name="login"
12      class="tutuorial.Login">
13          <interceptor-ref name="myStack"/>
14          <result name="input">login.jsp</result>
15          <result name="success"
16              type="redirectAction">/secure/home</result>
17 </action>
18 </package>

怎樣學php基礎。轉載于:https://www.cnblogs.com/niloay/p/struts2-interceptors.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/180251.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息