You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mike Kienenberger <mk...@alaska.net> on 2003/12/18 22:40:55 UTC

Can Velocity invoke a method in a public inner class?

Can Velocity reference a method in a public inner class?

I'm trying to call [String] getNameString() on an inner public class, but it 
never resolves.  Note that the implicit toString() method does work.  
Actually, I just tested, and an explicit toString() also works.   However, 
copying toString() to toX(), then calling toX() does NOT work.


 Velocity   [warn] org.apache.velocity.runtime.exception.ReferenceException: 
reference : template = /WEB-INF/common/ReportComponent.vm [line 53,column 
184] : ${currentFieldName.getNameString()} is not a valid reference.


template snippet:
################################
#foreach					($currentFieldName in $searchCriteriaFieldNameList)
<!-- currentFieldName = $currentFieldName -->
								<option 
value="$velocityCount"#if("$velocityCount"=="${SearchForm.FieldNamePopUpButtonIndex}") 
selected="selected"#end>${currentFieldName.getNameString()}</option>
#end						#* PopUp FieldNamePopUpButtonIndex *#
################################


Output snippet:
################################
<!-- currentFieldName = 
com.gvea.struts.utils.ReportComponent$ColumnMetaData@627b5c: { 
attributeString=Select Field Name..., nameString=Select Field Name..., 
dataType=null, formatter=null, selected=null } -->
								<option value="0">${currentFieldName.getNameString()}</option>
<!-- currentFieldName = 
com.gvea.struts.utils.ReportComponent$ColumnMetaData@190d536: { 
attributeString=accountNumber, nameString=Acct #, dataType=Number, 
formatter=null, selected=true } -->
								<option value="1">${currentFieldName.getNameString()}</option>
################################



Inner class declaration:
################################
    class ColumnMetaData extends Object
    {
        protected String attributeString;
		protected String nameString;
		protected Object dataType;
		protected Object formatter;
		protected Boolean selected;

        public ColumnMetaData(
            String newattributeString,
            String newNameString,
            Object newDataType,
            Object newFormatterString,
            Boolean isSelected)
        {
            super();
            attributeString = newattributeString;
            nameString = newNameString;
            dataType = newDataType;
            formatter = newFormatterString;
            selected = isSelected;
        }

		public String getAttributeString() { return attributeString; }
		public String getNameString() { return nameString; }
		public Object getDataType() { return dataType; }
		// public Object getFormatter() { return formatter; }
		public Object getSelected() { return selected; }

        public java.text.Format getFormatter()
        {
            if (null == formatter)
                return null;
            if (formatter instanceof java.text.Format)
                return (java.text.Format)formatter;

            if (formatter instanceof String)
            {
                if (DATA_TYPE_NUMBER == dataType)
                    return new DecimalFormat((String)formatter);
                else if (DATA_TYPE_DATE == dataType)
                    return new SimpleDateFormat((String)formatter);
                else
                    return null;
            }
            else
                return null;
        }
        
        public String toString()
        {
        	return super.toString()
        		+ ": { attributeString=" + attributeString
				+ ", nameString=" + nameString
				+ ", dataType=" + dataType
				+ ", formatter=" + formatter
				+ ", selected=" + selected
				+ " }";
        }
    }
################################

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can Velocity invoke a method in a public inner class?

Posted by Mike Kienenberger <mk...@alaska.net>.
Nathan Bubna <na...@esha.com> wrote:
> try:
> 
> public class ColumnMetaData
> {
> ...
> }
> 
> both class and method have to be public to keep Velocity happy. :)

Duh.  Yeah, that's obvious in retrospect.  Stupid mistake on my part.

I hate the fact that declarations default to private rather than requiring 
an explicit declaration in java.

Thanks for noticing that!

-Mike

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can Velocity invoke a method in a public inner class?

Posted by Nathan Bubna <na...@esha.com>.
Mike Kienenberger said:
> Can Velocity reference a method in a public inner class?
...
> Inner class declaration:
> ################################
>     class ColumnMetaData extends Object
> 
>     {
...
>     }
> 
> ################################


try:

public class ColumnMetaData
{
...
}

both class and method have to be public to keep Velocity happy. :)

Nathan Bubna
nathan@esha.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org