没有数据从Java文件提取到Velocity宏文件

时间:2014-05-02 06:43:35

标签: java velocity

我在java文件中有以下代码,其中HashMap存储一些数据,我希望它在vm文件中显示为模板。 包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文件看起来像这样

No of student is : $stuDetails.size()

Details of student

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

#end

为什么没有数据从HashMap提取到vm文件中? 下面是我的控制台输出

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

2 个答案:

答案 0 :(得分:0)

您的代码中没有方法,这就是导致语法错误的原因。您调用是在声明部分,将它们移动到方法中。好像

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);
  }
}

如果你想用它作为主要方法。

答案 1 :(得分:0)

我认为访问地图值的方式是[]表示法:

No of student is : $stuDetails.size()

Details of student

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

#end