Domanda

I have the following code in java file in which a HashMap is storing some data and i want it to be displayed as a template in a vm file. package gt;

import java.io.StringWriter;
import java.util.*;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Stu {
     VelocityEngine ve = new VelocityEngine();
      ve.init();

      @SuppressWarnings("rawtypes")
    ArrayList list = new ArrayList();

      Map map = new HashMap();
     map = new HashMap();
      map.put("rno", "2");
      map.put("name", "Komal");
      map.put("cla", "Bca");
      list.add(map);

      VelocityContext context = new VelocityContext();
      context.put("stuDetails", list);
      context.put("Name", new Stu());

      Template t = ve.getTemplate("VMFile/stu.vm");
      StringWriter writer = new StringWriter();
      t.merge(context, writer);

      System.out.println(writer);
      }
}

vm file looks like this

No of student is : $stuDetails.size()

Details of student

#foreach( $stuu in $stuDetails ) 
Roll No - $stuu.rno 
Name - $stuu.name 
Class - $stuu.cla >

#end

Why no data is fetched from HashMap into vm file? Below is my console output

May 02, 2014 12:29:42 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\DMIX;C:\Program Files\nodejs\;C:\Users\deepakgopal\AppData\Roaming\npm;.
May 02, 2014 12:29:42 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:dee' did not find a matching property.
May 02, 2014 12:29:42 PM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 02, 2014 12:29:42 PM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 02, 2014 12:29:42 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 355 ms
May 02, 2014 12:29:42 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 02, 2014 12:29:42 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
May 02, 2014 12:29:43 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 02, 2014 12:29:43 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 02, 2014 12:29:43 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1221 ms
È stato utile?

Soluzione

You have no method in your code, that is what causes your syntax error. You calls are in the declaration section, move them into a method. Sth like

import java.io.StringWriter;
import java.util.*;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Stu {

  public static void main(String[] argv) {
    VelocityEngine ve = new VelocityEngine();
    Properties p = new Properties();
    p.setProperty("resource.loader", "file");
    p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
    p.setProperty("file.resource.loader.path", "***INSERT TEMPLATEDIR***");
    p.setProperty("file.resource.loader.cache", "false");
    p.setProperty("file.resource.loader.modificationCheckInterval", "0");
    ve.init(p);
    @SuppressWarnings("rawtypes")
    ArrayList list = new ArrayList();

    Map map = new HashMap();
    map.put("rno", "2");
    map.put("name", "Komal");
    map.put("cla", "Bca");
    list.add(map);

    VelocityContext context = new VelocityContext();
    context.put("stuDetails", list);

    Template t = ve.getTemplate("stu.vm");
    StringWriter writer = new StringWriter();
    t.merge(context, writer);

    System.out.println(writer);
  }
}

if you want to use it as main method.

Altri suggerimenti

I think the way of accessing the values of a map is with the [] notation:

No of student is : $stuDetails.size()

Details of student

#foreach( $stuu in $stuDetails ) 
Roll No - $stuu["rno"] 
Name - $stuu["name"]
Class - $stuu["cla"] >

#end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top