alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (RepopulateConversionErrorFieldValidatorSupport.java)

This example Struts source code file (RepopulateConversionErrorFieldValidatorSupport.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

actioninvocation, linkedhashmap, log, map, map, object, object, preresultlistener, repopulateconversionerrorfieldvalidatorsupport, string, string, util, validationexception, validationexception, valuestack

The Struts RepopulateConversionErrorFieldValidatorSupport.java source code

/*
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opensymphony.xwork2.validator.validators;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import com.opensymphony.xwork2.validator.ValidationException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 
 * 
 * An abstract base class that adds in the capability to populate the stack with
 * a fake parameter map when a conversion error has occurred and the 'repopulateField'
 * property is set to "true".
 * 
 * <p/>
 * 
 *
 * <!-- START SNIPPET: javadoc -->
 *
 * The capability of auto-repopulating the stack with a fake parameter map when 
 * a conversion error has occurred can be done with 'repopulateField' property 
 * set to "true". 
 *
 * <p/>
 *
 * This is typically usefull when one wants to repopulate the field with the original value 
 * when a conversion error occurred. Eg. with a textfield that only allows an Integer 
 * (the action class have an Integer field declared), upon conversion error, the incorrectly
 * entered integer (maybe a text 'one') will not appear when dispatched back. With 'repopulateField'
 * porperty set to true, it will, meaning the textfield will have 'one' as its value 
 * upon conversion error.
 * 
 * <!-- END SNIPPET: javadoc -->
 * 
 * <p/>
 * 
 * <pre>
 * <!-- START SNIPPET: exampleJspPage -->
 * 
 * <!-- myJspPage.jsp -->
 * <ww:form action="someAction" method="POST">
 *   ....
 *   <ww:textfield 
 *       label="My Integer Field"
 *       name="myIntegerField" />
 *   ....
 *   <ww:submit />       
 * </ww:form>
 * 
 * <!-- END SNIPPET: exampleJspPage -->
 * </pre>
 * 
 * <pre>
 * <!-- START SNIPPET: exampleXwork -->
 * 
 * <!-- xwork.xml -->
 * <xwork>
 * <include file="xwork-default.xml" />
 * ....
 * <package name="myPackage" extends="xwork-default">
 *   ....
 *   <action name="someAction" class="example.MyActionSupport.java">
 *      <result name="input">myJspPage.jsp</result>
 *      <result>success.jsp</result>
 *   </action>
 *   ....
 * </package>
 * ....
 * </xwork>
 * 
 * <!-- END SNIPPET:exampleXwork -->
 * </pre>
 * 
 * 
 * <pre>
 * <!-- START SNIPPET: exampleJava -->
 * 
 * <!-- MyActionSupport.java -->
 * public class MyActionSupport extends ActionSupport {
 *    private Integer myIntegerField;
 *    
 *    public Integer getMyIntegerField() { return this.myIntegerField; }
 *    public void setMyIntegerField(Integer myIntegerField) { 
 *       this.myIntegerField = myIntegerField; 
 *    }
 * }
 * 
 * <!-- END SNIPPET: exampleJava -->
 * </pre>
 * 
 * 
 * <pre>
 * <!-- START SNIPPET: exampleValidation -->
 * 
 * <!-- MyActionSupport-someAction-validation.xml -->
 * <validators>
 *   ...
 *   <field name="myIntegerField">
 *      <field-validator type="conversion">
 *         <param name="repopulateField">true</param>
 *         <message>Conversion Error (Integer Wanted)</message>
 *      </field-validator>
 *   </field>
 *   ...
 * </validators>
 * 
 * <!-- END SNIPPET: exampleValidation -->
 * </pre>
 * 
 * @author tm_jee
 * @version $Date: 2009-12-27 19:18:29 +0100 (Sun, 27 Dec 2009) $ $Id: RepopulateConversionErrorFieldValidatorSupport.java 894090 2009-12-27 18:18:29Z martinc $
 */
public abstract class RepopulateConversionErrorFieldValidatorSupport extends FieldValidatorSupport {
	
	private static final Logger LOG = LoggerFactory.getLogger(RepopulateConversionErrorFieldValidatorSupport.class);
	
	private String repopulateFieldAsString = "false";
	private boolean repopulateFieldAsBoolean = false;
	
	public String getRepopulateField() { 
		return repopulateFieldAsString;
	}
	
	public void setRepopulateField(String repopulateField) {
		this.repopulateFieldAsString = repopulateField == null ? repopulateField : repopulateField.trim();
		this.repopulateFieldAsBoolean = "true".equalsIgnoreCase(this.repopulateFieldAsString) ? (true) : (false);
	}

	public void validate(Object object) throws ValidationException {
		doValidate(object);
		if (repopulateFieldAsBoolean) {
			repopulateField(object);
		}
	}
	
	public void repopulateField(Object object) throws ValidationException {
		
		ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
		Map<String, Object> conversionErrors = ActionContext.getContext().getConversionErrors();
		
		String fieldName = getFieldName();
		String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
        if (conversionErrors.containsKey(fullFieldName)) {
            Object value = conversionErrors.get(fullFieldName);

            final Map<Object, Object> fakeParams = new LinkedHashMap();
            boolean doExprOverride = false;

            if (value instanceof String[]) {
                // take the first element, if possible
                String[] tmpValue = (String[]) value;
                if (tmpValue != null && (tmpValue.length > 0)) {
                    doExprOverride = true;
                    fakeParams.put(fullFieldName, "'" + tmpValue[0] + "'");
                } else {
                    LOG.warn("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error ");
                }
            } else if (value instanceof String) {
                String tmpValue = (String) value;
                doExprOverride = true;
                fakeParams.put(fullFieldName, "'" + tmpValue + "'");
            } else {
                // opps... it should be 
                LOG.warn("conversion error value is not a String or array of String but instead is [" + value + "], will not repopulate conversion error");
            }

            if (doExprOverride) {
                invocation.addPreResultListener(new PreResultListener() {
                    public void beforeResult(ActionInvocation invocation, String resultCode) {
                        ValueStack stack = ActionContext.getContext().getValueStack();
                        stack.setExprOverrides(fakeParams);
                    }
                });
            }
        }
	}
	
	protected abstract void doValidate(Object object) throws ValidationException;
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts RepopulateConversionErrorFieldValidatorSupport.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.