Question

I want to generate below code snippet using Velocity.


    Map<String, List<String>> fruitsAndTypesMap = new HashMap<String, List<String>>();
    String fruit_Apple = "Apple";
    List<String> types_Apple = new ArrayList<String>();
        types_Apple.add("KA");
        types_Apple.add("WA");
    fruitsAndTypesMap.put(fruit_Apple, types_Apple);    

    String fruit_Orange = "Orange"; 
    List<String> types_Orange = new ArrayList<String>();
        types_Orange.add("HO");
        types_Orange.add("LO");
    fruitsAndTypesMap.put(fruit_Orange, types_Orange);

My template_fruits.vm file is as below.


#if ($fruitsMap.size()>0)
    Map<String, List<String>> fruitsAndTypesMap = new HashMap<String,List<String>>();
    #foreach( $fruitName in $fruitsMap.keySet() )
        String fruit_$fruitName = "$fruitName";
        List<String> types_$fruitName = new ArrayList<String>();
            #foreach( $fruitType in $fruitsMap.get($fruitName) )
                     types_$fruitName.add("$fruitType.name");
            #end
        fruitsAndTypesMap.put(fruit_$fruitName, types_$fruitName);
    #end
#end    

Issue is with the statement: types_$fruitName.add("$fruitType.name"); it does not evaluate the $fruitName properly. But if i modify the statement to be like: types_($fruitName).add("$fruitType.name");, it is evaluated properly but the value is surrounded with brackets. I dont understand the brackets trick for evaluation.

Below code can be used to load $fruitsMap


      public static Map<String, List<Fruit>> getFruitsMap(){

    Map<String, List<Fruit>> fruitsMap = new HashMap<String, List<Fruit>>();
    List<Fruit> applesList = new ArrayList<Fruit>();
    Fruit fruit_Apple = null; 
        fruit_Apple = new Fruit();  fruit_Apple.setName("KA"); 
    applesList.add(fruit_Apple);
        fruit_Apple = new Fruit();  fruit_Apple.setName("WA");
    applesList.add(fruit_Apple);

    List<Fruit> orangesList = new ArrayList<Fruit>();
    Fruit fuit_Orange = null;
        fuit_Orange = new Fruit();  fuit_Orange.setName("HO");
    orangesList.add(fuit_Orange);
        fuit_Orange = new Fruit();  fuit_Orange.setName("LO");
    orangesList.add(fuit_Orange);

    fruitsMap.put("Apple", applesList);
    fruitsMap.put("Orange", orangesList);
    return fruitsMap;
}

Code related to template execution:


VelocityEngine velEngine = new VelocityEngine();
        velEngine.init();
    Template template = velEngine.getTemplate("template_fruits.vm");
    VelocityContext context = new VelocityContext();
        context.put("fruitsMap", FruitClient.getFruitsMap());
    StringWriter writer = new StringWriter();
        template.merge(context, writer);
    System.out.println("Content: " + writer.toString());    

Any hints of what is the wrong with the statement--> types_$fruitName.add("$fruitType.name"); will be helpful.

Thanks

Was it helpful?

Solution

The statement should be:

types_${fruitName}.add("$fruitType.name");

Otherwise, Velocity tries to call an add method on the $fruitName object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top