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

Struts example source code file (ConditionalVisitorFieldValidator.java)

This example Struts source code file (ConditionalVisitorFieldValidator.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

boolean, boolean, conditionalvisitorfieldvalidator, exception, got, object, object, override, string, string, validationexception, validationexception, visitorfieldvalidator

The Struts ConditionalVisitorFieldValidator.java source code

package com.opensymphony.xwork2.validator.validators;

import com.opensymphony.xwork2.validator.ValidationException;

/**
 * <code>ConditionalVisitorFieldValidator
 *
 *
 * <field name="colleaguePosition">
 *   <field-validator type="fieldexpression" short-circuit="true">
 *     reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
 *     <message>You must choose a position where you worked with this person,
 * or choose "Other..."</message>
 *   </field-validator>
 *   <field-validator type="conditionalvisitor">
 *     reason == 'colleague' and colleaguePositionID == 'OTHER'
 *     <message/>
 *   </field-validator>
 * </field>
 *
 * @author Matt Raible
 */
public class ConditionalVisitorFieldValidator extends VisitorFieldValidator {
    private String expression;

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public String getExpression() {
        return expression;
    }

    /**
     * If expression evaluates to true, invoke visitor validation.
     *
     * @param object the object being validated
     * @throws ValidationException
     */
    @Override
    public void validate(Object object) throws ValidationException {
        if (validateExpression(object)) {
            super.validate(object);
        }
    }

    /**
     * Validate the expression contained in the "expression" paramter.
     *
     * @param object the object you're validating
     * @return true if expression evaluates to true (implying a validation
     *         failure)
     * @throws ValidationException if anything goes wrong
     */
    public boolean validateExpression(Object object) throws ValidationException {
        Boolean answer = Boolean.FALSE;
        Object obj = null;

        try {
            obj = getFieldValue(expression, object);
        }
        catch (ValidationException e) {
            throw e;
        }
        catch (Exception e) {
            // let this pass, but it will be logged right below
        }

        if ((obj != null) && (obj instanceof Boolean)) {
            answer = (Boolean) obj;
        } else {
            log.warn("Got result of " + obj + " when trying to get Boolean.");
        }

        return answer;
    }
} 

Other Struts examples (source code examples)

Here is a short list of links related to this Struts ConditionalVisitorFieldValidator.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.