Search in sources :

Example 6 with PreResultListener

use of com.opensymphony.xwork2.interceptor.PreResultListener in project struts by apache.

the class RepopulateConversionErrorFieldValidatorSupport method repopulateField.

public void repopulateField(Object object) throws ValidationException {
    ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
    Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
    String fieldName = getFieldName();
    String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
    if (conversionErrors.containsKey(fullFieldName)) {
        Object value = conversionErrors.get(fullFieldName).getValue();
        final Map<Object, Object> fakeParams = new LinkedHashMap<Object, Object>();
        boolean doExprOverride = false;
        if (value instanceof String[]) {
            // take the first element, if possible
            String[] tmpValue = (String[]) value;
            if ((tmpValue.length > 0)) {
                doExprOverride = true;
                fakeParams.put(fullFieldName, escape(tmpValue[0]));
            } else {
                LOG.warn("value is an empty array of String or with first element in it as null [{}], will not repopulate conversion error", value);
            }
        } else if (value instanceof String) {
            String tmpValue = (String) value;
            doExprOverride = true;
            fakeParams.put(fullFieldName, escape(tmpValue));
        } else {
            // opps... it should be
            LOG.warn("conversion error value is not a String or array of String but instead is [{}], will not repopulate conversion error", value);
        }
        if (doExprOverride) {
            invocation.addPreResultListener(new PreResultListener() {

                public void beforeResult(ActionInvocation invocation, String resultCode) {
                    ValueStack stack = ActionContext.getContext().getValueStack();
                    stack.setExprOverrides(fakeParams);
                }
            });
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)5 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)3 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)1 WithLazyParams (com.opensymphony.xwork2.interceptor.WithLazyParams)1 MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)1 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)1 OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1