In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to analyze the use of Struts2 interceptor, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Struts2's interceptor is similar to a Servlet filter. Before executing the execute method of Action, Struts2 first executes the interceptor referenced in struts.xml, and after executing the intercept method of all referenced interceptors, it executes the execute method of Action.
The Struts2 interceptor class must inherit from the com.opensymphony.xwork2.interceptor.Interceptor interface, and there are three methods to implement in the Intercepter interface:
Void destroy (); void init (); String intercept (ActionInvocation invocation) throws Exception
The intercept method is the core method of the interceptor, which is called by all installed interceptors. Some built-in interceptors have been predefined in struts-default.xml in Struts2, such as timer, params, and so on. If in
< package>If struts-default is inherited in the tag, the current package automatically owns all the configurations in the struts-default.xml. The code is as follows:
< package name="demo" extends="struts-default" >...
< /package>There is a default reference in struts-default.xml by default (that is,
< action>Some interceptors are automatically referenced when the interceptor is not referenced in. The default interceptor reference is as follows:
< default-interceptor-ref name="defaultStack"/> < interceptor-stack name="defaultStack"> < interceptor-ref name="exception"/> < interceptor-ref name="alias"/> < interceptor-ref name="servletConfig"/> < interceptor-ref name="prepare"/> < interceptor-ref name="i18n"/> < interceptor-ref name="chain"/> < interceptor-ref name="debugging"/> < interceptor-ref name="profiling"/> < interceptor-ref name="scopedModelDriven"/> < interceptor-ref name="modelDriven"/> < interceptor-ref name="fileUpload"/> < interceptor-ref name="checkbox"/> < interceptor-ref name="staticParams"/> < interceptor-ref name="params"> < param name="excludeParams">Dojo\.. *
< /param> < /interceptor-ref> < interceptor-ref name="conversionError"/> < interceptor-ref name="validation"> < param name="excludeMethods">Input,back,cancel,browse
< /param> < /interceptor-ref> < interceptor-ref name="workflow"> < param name="excludeMethods">Input,back,cancel,browse
< /param> < /interceptor-ref> < /interceptor-stack>All the interceptors referenced in defaultStack above can be found in the
< action>Can be used without reference in the
< action>After any interceptor is referenced in, to use the interceptor defined in defaultStack, you also need to use the interceptor in the
< action>, which will be explained in more detail later).
Let's take a look at some simple ways to use interceptors.
1. Record the execution time of interceptors and execute methods (timer)
Timer is the simplest interceptor in Struts2, and the corresponding class for this interceptor is com.opensymphony.xwork2.interceptor.TimerInterceptor. Its function is to record the sum of the execution time of the intercept methods of the execute method and other interceptors (interceptors defined after timer). As shown in the following configuration code:
< action name="first" class="action.FirstAction"> < interceptor-ref name="logger"/> < interceptor-ref name="timer" /> < /action>Since there is no other interceptor definition after timer, timer can only record the execution time of the execute method, and when accessing the first action, it will output a message similar to the following in the console:
Information: Executed action [/ testample firstbank execute] took 16 ms.
Commons-logging.jar support is required when using the timer interceptor. By placing the logger reference after the timer, you can record the sum of the execution time of the intercept method of the logger interceptor and the execute method of the Action. The code is as follows:
< action name="first" class="action.FirstAction"> < interceptor-ref name="timer" /> < interceptor-ref name="logger"/> < /action>You can test the timer interceptor with the following Action class:
Package action; import com.opensymphony.xwork2.ActionSupport; public class FirstAction extends ActionSupport {public String execute () throws Exception {Thread.sleep (1000); / / delay 1 second return null;}}
If you record only the execution time of the execute method, you will generally output the following information:
Information: Executed action [/ Testram firstworthy execute] took 1000 ms.
2. Call the setter method (params) of Action by request
When a form of the client submits a request to the server, if there is a textfield, the code is as follows:
< s:form action="first" namespace="/test"> < s:textfield name="name"/> < s:submit/> < /s:form>After the submission, Struts2 will automatically call the setName method in the first action class and pass in the value in the name text box through the parameters of the setName method. In fact, this operation is done by the params interceptor, and the corresponding class of params is com.opensymphony.xwork2.interceptor.ParametersInterceptor. Because params is already defined in defaultStack, the interceptor is not referenced
< action>Params is automatically referenced in, as in the following configuration code, Struts2 automatically executes the corresponding setter method when accessing the first action.
< action name="first" class="action.FirstAction">......
< /action>But if it's in
< action>If other interceptors are referenced in, the params interceptor must be referenced again before Struts2 can call the corresponding setter method. As shown in the following configuration code:
< action name="first" class="action.FirstAction"> < interceptor-ref name="timer" /> < interceptor-ref name="params"/> < /action>3. Call the setter method (static-params) of Action through configuration parameters.
The static-params interceptor can be configured by
< params>Tag to call the corresponding setter method of the Action class, and the corresponding class of the static-params interceptor is com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.
The following configuration code demonstrates how to use the static-params interceptor:
< action name="first" class="action.FirstAction"> < interceptor-ref name="timer" /> < param name="who">Bill
< /param> < interceptor-ref name="params"/> < interceptor-ref name="static-params"/> < /action>If the first action uses the above configuration, when accessing the first action, Struts2 automatically calls the setWho method to pass "Bill" as the parameter value to the setWho method.
Fourth, use the interceptor stack
In order to easily reference the same or more interceptors in multiple actions, you can use the interceptor stack to reference these interceptors as a whole. The interceptor stack needs to be located in the
< package>Used in label
< interceptors>And sub-tag
< interceptor-stack>To define. The code is as follows:
< package name="demo" extends="struts-default" > < interceptors> < interceptor-stack name="mystack"> < interceptor-ref name="timer" /> < interceptor-ref name="logger" /> < interceptor-ref name="params" /> < interceptor-ref name="static-params" /> < /interceptor-stack> < /interceptors> < action name="first" class="action.FirstAction"> < param name="who">Bill
< /param> < interceptor-ref name="mystack"/> < /action> < /package>The interceptor stack can be used like an interceptor, as shown in the code above.
On how to use Struts2 interceptor analysis to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 206
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.