Struts2 18 Interceptor (17th)

Source: Internet
Author: User

The StrutsConversionErrorInterceptor interceptor is at the position of defastack stack 16th and is used to handle type conversion errors. The interceptor inherits from ConversionErrorInterceptor and most of its functional logic is in ConversionErrorInterceptor, strutsConversionErrorInterceptor only overwrites the two methods, namely the getOverrideExpr and shouldAddError methods. Because the ConversionErrorInterceptor class is provided by xwrok, struts2 considers these two methods to be not so reasonable and thus overwrites them. Therefore, StrutsConversionErrorInterceptor calls the intercept method of the parent class ConversionErrorInterceptor. The following is the source code of this method:

@ Override public String intercept (ActionInvocation invocation) throws Exception {// get the ActionContext object ActionContext invocationContext = invocation. getInvocationContext (); // obtain the conversionErrors Map in the ActionContext Object. Therefore, the type conversion error information is stored in the Map. Map <String, Object> conversionErrors = invocationContext. getConversionErrors (); ValueStack stack = invocationContext. getValueStack (); // obtain the value stack HashMap <Object, Object> fakie = Null; // iteration type conversion error for (Map. entry <String, Object> entry: conversionErrors. entrySet () {String propertyName = entry. getKey (); // The Name Of The request parameter with incorrect conversion is Object value = entry. getValue (); // The request parameter value if (shouldAddError (propertyName, value) for incorrect conversion {// you can determine whether to add the error message String message = XWorkConverter. getConversionErrorMessage (propertyName, stack); // get the error message // get the current Action Object action = invocation. getAction (); if (action insta Nceof ValidationAware) {// only actions that implement the ValidationAware API ValidationAware va = (ValidationAware) Action; // Add the error information to the FieldError set, because the ValidationAware interface is not implemented, there is no addFieldError method va. addFieldError (propertyName, message);} if (fakie = null) {fakie = new HashMap <Object, Object> ();} // Add the error value to fakieMap (fakie indicates fake or fake) fakie. put (propertyName, getOverrideExpr (invocation, value) ;}}// if fakie has a value if (fakie! = Null) {// place the fakie Map to the ActionContext stack. getContext (). put (ORIGINAL_PROPERTY_OVERRIDE, fakie); // register a PreResultListener listener invocation. addPreResultListener (new PreResultListener () {public void beforeResult (ActionInvocation invocation, String resultCode) {Map <Object, Object> fakie = (Map <Object, Object>) invocation. getInvocationContext (). get (ORIGINAL_PROPERTY_OVERRIDE); if (fakie! = Null) {invocation. getStack (). setExprOverrides (fakie) ;}}) ;}return invocation. invoke (); // call the next interceptor}

 

Here, only actions that implement the ValidationAware interface can add the type conversion error information to FieldError and provide it to the last loader: DefaultWorkflowInterceptor, in addition, a PreResultListener listener will be registered for a type conversion error. The listener will be executed before the Result execution after the Action and interceptor are executed, this listener is used to obtain the fakie Map stored in ActionContext and call the setExprOverrides method setting of ValueStack for the value stack, when you get the parameter with this type conversion error through OGNL expression on the page, the original value of the parameter is returned. For example, there is a request parameter path? Age = xxx, which is received by the int type in the Action. In this case, a conversion error is reported. When you use OGNL on the page to obtain the age value (<s: property value = "age"/>) returns xxx instead of 0.

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.