Struts2 18 Interceptor (14th)

Source: Internet
Author: User

StaticParametersInterceptor: the interceptor is in the defaultStack 13th position. It is used to fill in the static parameters configured for Action in the struts2 configuration file through the <param> tag configuration. If Action implements the Parameterizable interface, static parameters will also be encapsulated into a Map and passed directly to the Action. These static parameters will be added to the request parameter Map, unless the interceptor's merge attribute is false, the default value of this attribute is true. The following is a static parameter configuration: [html] <action name = "staticparams" class = "com. xforwarfjpk. action. staticParametersAction "> <result>/WEB-INF/page/message. jsp </result> <param name = "username"> zhangjin </param> <param name = "password"> xforwarfjpk </param> </action> intercept Method source code: [java] <span style = "font-family: Courier New; font-size: 14px"> @ Override public String intercept (ActionInvocation invocation) throws Exception {Act IonConfig config = invocation. getProxy (). getConfig (); // get the Action configuration Object action = invocation. getAction (); // obtain the currently executed Action final Map <String, String> parameters = config. getParams (); // obtain the static parameter Map of the current Action. Note that it is not the request parameter Map // omitted... // if Action implements the Parameterizable interface if (action instanceof Parameterizable) {(Parameterizable) action ). setParams (parameters); // call the setParams method of Action and directly set it to Action} // if Action is configured Static parameter if (parameters! = Null) {ActionContext ac = ActionContext. getContext (); // obtain the ActionContext Object Map <String, Object> contextMap = ac. getContextMap (); // get the context Map object inside the ActionContext object, that is, the OgnlContext object try {// omitted... final ValueStack stack = ac. getValueStack (); // get ValueStack // omit... for (Map. entry <String, String> entry: parameters. entrySet () {// iterative static parameter Map Object val = entry. getValue (); // obtain the current parameter value if (parse & val instanceo F String) {// If parse is true, the parameter value must be calculated as an expression. The default value of parse is false val = TextParseUtil. translateVariables (val. toString (), stack); // Replace the returned value} try {// set the current parameter to newStack. setValue (entry. getKey (), val);} catch (RuntimeException e) {// omitting ...}} // omit... if (merge) // whether to merge, the default value is true addParametersToContext (ac, parameters); // Add static parameters to ActionContext} finally {// omitting ...}} return invocation. invoke (); // call the next interceptor} </ Span> some non-important code is omitted in the source code above. To view the complete source code, see the built-in source code of struts2. In fact, the logic of this method is relatively simple, that is, to fill in the static parameters in the Action configuration into the Action, this is through newStack. setValue (entry. getKey (), val); this code is implemented because Action generally processes the top of the ValueStack stack. If a parameter is received in the Action, the parameter value is assigned to the Action. If the merge (true by default) attribute of the interceptor is true, the static parameters will be added to the ActionContext. The following is the source code of the addParametersToContext method: [java] <span style = "font-family: Courier New; font-size: 14px"> protected void addParametersToContext (ActionContext ac, Map <String,?> NewParams) {Map <String, Object> previousParams = ac. getParameters (); // obtain the request parameter Map <String, Object> combinedParams; if (overwrite) {// whether to overwrite the request parameter if (previousParams! = Null) {combinedParams = new TreeMap <String, Object> (previousParams); // here the previousParams request parameter} else {combinedParams = new TreeMap <String, object> ();} if (newParams! = Null) {combinedParams. putAll (newParams); // put the static parameter again. if the key is the same, the request parameter overwrites the static parameter, which is equivalent to not overwriting the request parameter} else {if (newParams! = Null) {combinedParams = new TreeMap <String, Object> (newParams); // stores static parameters first} else {combinedParams = new TreeMap <String, Object> ();} if (previousParams! = Null) {combinedParams. putAll (previousParams); // repeat the request parameters. If the keys are the same, the static parameters overwrite the Request Parameters }}// set the processed parameters to ac in the ActionContext. setParameters (combinedParams);} </span> added the static parameters to the ActionContext through the above method. The logic of the interceptor is described here, and invocation is called. invoke (); call the next Interceptor ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.