Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity Dynamic Property Access

Tags:

java

velocity

Is it possible to dynamically access properties by using #evaluate? I apologize in advance for the length, but most of this is just example code to fully illustrate my issue.

I have a preferences class which looks like this:

public class DefaultUserPreferences implements Preferences {
    //Getters and setters left off for "brevity…"
    private Panel defaultPanel;
    private OrderByColumn mostActiveSortOrder;
    private OrderByColumn recentlyModifiedAccountsSortColumn;
}

Each of these types are simply a custom enum.

public enum OrderByColumn {
    NAME,
    LAST_ACTIVITY,
    GROUP
}

public enum Panel {
    MOST_ACTIVE,
    RECENTLY_MODIFIED;

    public String getCamelCase() {
        String[] words = StringUtils.split(this.name(), "_");
        String rval = StringUtils.EMPTY;
        if (words != null && words.length >= 1) {
            rval = StringUtils.lowerCase(words[0]);
            for(int i = 1; I < words.length; i++) {
               rval += StringUtils.capitalize(words[i].toLowerCase());
            }
        }
        return rval;
    }
}

Below is a snippet of how I’d like to display the preferences to the users, but I can’t seem to get the getter to be called (I get the following if I evaluate it to get text: test.core.model.entities.DefaultUserPreferences@596fde80.mostActiveSortOrder)

#for ($panel in $Panels)
    ## The names here are correct
    #set($selectName = ${panel.CamelCase}SortColumn) 
    #set($dynamicProperty = $preferences.$selectName)
    <tr>
        <td>$panel</td>
        <td>
            <select name="$selectName">
                #for($option in $OrderByColumn)
                    <option value="$option" #if($option == #evaluate($dynamicProperty) selected="selected" #end>$option</option>
                #end
       </td>
    </tr>
#end

However my getter never seems to be called on the preferences. I’ve added each of the pieces to the Context, and am not having any issues iterating over the Panels, I just can’t seem to get the syntax down to dynamically call the getters on properties. Is this possible in 1.7?

like image 428
Scott Avatar asked Jun 06 '11 19:06

Scott


People also ask

What is Property Velocity?

Velocity allows you to access properties through a short-hand notation. The objects to look up the properties must be available through a Velocity variable and the notation consists of a leading variable followed by the dot (" . ") character and another VTL Identifier.

What is .VM file in Java?

Developer file used by Velocity, a Java-based template engine; written using the Velocity Template Language (VTL); contains VTL statements inserted in a normal text document; often used for auto-generating Web source code and class skeletons.

What is Velocity .VM file?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.

What is Apache Velocity used for?

Apache Velocity first released in April 2001, is a Java-based template engine that provides a template language to reference objects defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a Web application (the model–view–controller design pattern).


1 Answers

Received this from the mailing list. Basically evaluate only returns a string for display instead of returning a value. Thus the set directive is required inside of the evaluated string.

#set($selectName = "${panel.CamelCase}SortColumn")
#set($dynamicProp = '#set( $selectedPreference = ' + '$preferences.' + $selectName)  + ' )')
#evaluate($dynamicProp)

With these lines run, I may then check the value of selectedPreference against the values I am iterating over.

like image 63
Scott Avatar answered Sep 18 '22 19:09

Scott