0

I'd like to access all properties of a certain model class within the template. If I do

VelocityContext context = new VelocityContext();
context.put("model", new MyModel());

and MyModel is like

public class MyModel {
    private String propertyA;
    private String propertyB;
    public String getPropertyA() { return propertyA; }
    public String getPropertyB() { return propertyB; }
}

then I need to specify the model's alias with every access of it's properties.

This is my template with properties like $model.propertyA and $model.propertyB.

What I would like to achieve is that the template variables doesn't need to specify model. as prefix for a certain context member, like so:

This is my template with properties like $propertyA and $propertyB.   

Every variable should be treated like a property of the given "root" object of type MyModel. Is this possible and if yes, how?

Zeemee
  • 10,486
  • 14
  • 51
  • 81
  • check this link:https://stackoverflow.com/questions/17068607/how-to-access-an-objects-public-fields-from-a-velocity-template – soorapadman Jun 22 '17 at 08:27
  • @soorapadman thank, I read that before and I think these are different topics. – Zeemee Jun 22 '17 at 08:55
  • ok fine .I thought it may help you sorry . – soorapadman Jun 22 '17 at 09:00
  • This is not possible with Velocity - there is no way to specify a 'default' object. Property and method references will only be parsed correctly if preceded by the name of their object. – Markus Fischer Jun 22 '17 at 09:50
  • @MarkusFischer Thank you for your reply, and since this is a valid answer to my question, would you post it as such and allow me to accept it. :) – Zeemee Jun 22 '17 at 12:46
  • @Zeemee Thanks - just posted it as an answer... – Markus Fischer Jun 22 '17 at 13:13
  • Why don't you just put the properties at the root instead of the model? like `context.put("propertyA", new MyModel().getPropertyA);` – Tezra Jun 22 '17 at 13:17
  • @Tezra Your way too broad OP wants to create map for each property. – soorapadman Jun 22 '17 at 15:25
  • @soorapadman, As I understand the OP, he has A MAP of properties, that he would like to promote the properties of up out of the map. And I'm suggesting he just explicitly do that in the Java code. (I don't understand/see what you mean by "create map for each property") – Tezra Jun 22 '17 at 15:36
  • @Tezra Yes, indeed I *could* do this, but this is exactly what I'd like to avoid. I want to use the evaluation mechanism (`model.propertyA` looks for `model.getPropertyA()`) also for top level properties. I don't want to name/repeat every property of my "root" object to make it available to the context. – Zeemee Jun 23 '17 at 06:35
  • @Zeemee I'm not sure exactly what your're saying... You could use reflection to auto-load the object and its properties into the context. Or in the Velocity itself, you could unpack it using [dynamic variables](https://stackoverflow.com/questions/17084542/construct-variable-names-dynamically-in-velocity). Either way, to do this, somewhere somehow you need to unpack the model into the context to make its' properties available. My first comment was just saying "To not use the model reference, just add that property directly to the context" – Tezra Jun 23 '17 at 12:12
  • @Tezra Ok, reflection would be one way, but I thought I could use the reflection/property evaluation mechanism that is built-in in Velocity somehow. – Zeemee Jun 23 '17 at 16:36

1 Answers1

1

This is not possible with Velocity - there is no way to specify a 'default' object. Property and method references will only be parsed correctly if preceded by the name of their object.

Markus Fischer
  • 1,326
  • 8
  • 13