Struts 2 crée son propre intercepteur
Téléchargez-le -Struts2-Create-Own-Interceptor-Example.zip
Dans ce tutoriel, il montre comment créer un propre intercepteur dans Struts 2.
Étapes récapitulatives:
-
Créer une classe implémentecom.opensymphony.xwork2.interceptor.Interceptor.
-
Implémentez la méthodeintercept(ActionInvocation invocation).
-
Configurez l'intercepteur dans lesstruts.xml.
-
Reliez-le à l'action.
Struts 2 interceptors
Struts 2 est livré avec de nombreux intercepteurs prêts, assurez-vous de vérifier la liste desavailable Struts 2 interceptors avant de créer votre propre intercepteur.
Un exemple complet pour créer un propre intercepteur:
1. action
Une action simple pour transmettre la demande de l'utilisateur et imprimer un message.
HelloAction.java
package com.example.common.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport{ public String execute() throws Exception { System.out.println("HelloAction execute() is called"); return SUCCESS; } }
2. Intercepteur
Un exemple d'intercepteur complet.
PrintMsgInterceptor.java
package com.example.common.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class PrintMsgInterceptor implements Interceptor{ //called during interceptor destruction public void destroy() { System.out.println("CustomInterceptor destroy() is called..."); } //called during interceptor initialization public void init() { System.out.println("CustomInterceptor init() is called..."); } //put interceptor code here public String intercept(ActionInvocation invocation) throws Exception { System.out.println("CustomInterceptor, before invocation.invoke()..."); String result = invocation.invoke(); System.out.println("CustomInterceptor, after invocation.invoke()..."); return result; } }
Explanation
La classe d'intercepteur doit implémenter lescom.opensymphony.xwork2.interceptor.Interceptor interface. Lors de l'initialisation de l'intercepteur,init() est appelé; destruction de l'intercepteur,destroy() est appelé. Enfin, placez tout le code d'intercepteur qui fait le travail dans la méthodeintercept(ActionInvocation invocation).
invocation.invoke()
Dans la méthode intercepteur intercept (), vousmust called the invocation.invoke() et renvoyez le résultat. Il s'agit de la méthode responsable de l'appel du prochain intercepteur ou de l'action. L'action échouera sans appeler la méthodeinvocation.invoke().
destroy() is not reliable
Il n'est pas recommandé de mettre de code dans lesdestroy(), car cette méthode n'est pas fiable. Lorsque votre serveur d'applications est arrêté de force ou est tué par une commande, lesdestroy() ne seront pas appelés.
3. struts.xml
Configurez l'intercepteur dans le fichierstruts.xml.
pages/hello.jsp
4. Demo
Lors de l'initialisation du serveur, la méthode de l'intercepteurinit() est appelée.
INFO: Overriding property struts.i18n.reload - old value: false new value: true 15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true CustomInterceptor init() is called... 15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080 15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/20 config=null 15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 994 ms
Pendant que vous accédez à l'action via l'URL:http://localhost:8080/Struts2Example/helloAction.action
INFO: Overriding property struts.i18n.reload - old value: false new value: true 15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true CustomInterceptor init() is called... 15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080 15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/20 config=null 15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 994 ms CustomInterceptor, before invocation.invoke()... HelloAction execute() is called CustomInterceptor, after invocation.invoke()...