FileDocCategorySizeDatePackage
UsePropertyTag.javaAPI DocExample5023Thu Jun 28 16:14:16 BST 2001com.ora.jsp.tags.generic

UsePropertyTag

public class UsePropertyTag extends TagSupport
This class is a custom action for making a bean property value available to other actions and scripting code as a variable. The property must be a multi-value property. The getter method may take a String argument identifying the one value to return.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private String
id
private String
name
private String
property
private String
arg
private String
className
Constructors Summary
Methods Summary
public intdoEndTag()
Retrieves one value of a multi-valued property in the specified bean using a getter method with a String argument. The value is saved with the specified variable name in the page scope.

        Object obj = pageContext.findAttribute(name);
        if (obj == null) {
            throw new JspException("Variable " + name + " not found");
        }
        Object propObj = getProperty(obj, property, className);
        pageContext.setAttribute(id, propObj);
    	return SKIP_BODY;
    
private java.lang.ObjectgetProperty(java.lang.Object bean, java.lang.String property, java.lang.String propClassName)
Returns the value of the specified property from the specified bean.

param
bean the bean
param
property the property name
param
propertyClassName the property class name (type)

        Object propObj = null;
        Class beanClass = bean.getClass();
        Class[] params = null;
        if (arg != null) {
            // If an arg is specified, it must be a String arg
            params = new Class[1];
            params[0] = String.class;
        }
        Method method = null;
        try {
            /*
             * Since the method may have an arg, look for it explicitly
             * instead of using the standard property lookup method
             */
            method = beanClass.getMethod("get" +
                property.substring(0, 1).toUpperCase() + property.substring(1),
                params);
        }
        catch (NoSuchMethodException e) {
            throw new JspException("A property getter for " + property +
                (arg != null ? " with a String argument" : "") +
                " not found in " + name);
        }
        Class propClass = null;
        try {
          propClass = Class.forName(propClassName);
        }
        catch (ClassNotFoundException e) {
            throw new JspException("Property class " + propClassName + " not found");
        }

        Class returnType = method.getReturnType();
        if (!propClass.isAssignableFrom(returnType)) {
            throw new JspException("Property " + property + " is not a " + className);
        }
        Object[] args = null;
        if (arg != null) {
            args = new Object[1];
            args[0] = arg;
        }
        try {
            propObj = method.invoke(bean, args);
        }
        catch (Exception e) {
            throw new JspException("Failed to get property " + property + " from " + name);
        }
        return propObj;
    
public voidrelease()
Releases all instance variables.

        id = null;
        name = null;
        property = null;
        arg = null;
        className = null;
        super.release();
    
public voidsetArg(java.lang.String arg)
Sets the arg attribute, i.e. the String argument value used by the property getter method to select on of multiple property values.

param
arg the String argument value

        this.arg = arg;
    
public voidsetClassName(java.lang.String className)
Sets the class attribute, i.e. the class name for the property value.

param
className the property class name

        this.className = className;
    
public voidsetId(java.lang.String id)
Sets the id attribute, i.e. the name of the variable to hold the reference to the selected property value.

param
id the variable name

        this.id = id;
    
public voidsetName(java.lang.String name)
Sets the name attribute, i.e. the name of the variable holding the reference to the bean with the property to retrieve.

param
name the bean name

        this.name = name;
    
public voidsetProperty(java.lang.String property)
Sets the property attribute, i.e. the name of the property to retrieve.

param
property the property name

        this.property = property;