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?
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With