Java 如何在velocity模板文件中访问地图

Java 如何在velocity模板文件中访问地图,java,velocity,Java,Velocity,当我在velocity模板文件中传递Map时,当尝试打印Map的值时,它会被排序(基于ASCII值) 我的工作如下: 这是我的velocity模板文件:: #set($tocList=${mapReference.mapValue}) #set($tocEntry="") <div > #foreach($tocEntry in $tocList.keySet()) <a href="#$tocEntry">$tocList.get($tocEntry)</

当我在velocity模板文件中传递
Map
时,当尝试打印Map的值时,它会被排序(基于ASCII值)

我的工作如下:

这是我的velocity模板文件::

#set($tocList=${mapReference.mapValue})
#set($tocEntry="")

<div >
 #foreach($tocEntry in $tocList.keySet())
  <a href="#$tocEntry">$tocList.get($tocEntry)</a><br/>
 #end
</div>
#set($tocList=${mapReference.mapValue})
#set($toccentry=”“)
#foreach($tocList.keySet()中的tocEntry)

#结束
我的Java代码是:

 Map<String, String> map=new HashMap<String, String>();
 Map<String,HashMap> m1=new HashMap<String, HashMap>();

   \\values that we want to print in template file

   map.put("sdfhfg", "Df lm"); 
   map.put("chdgfhd", "gBc Jk");
   map.put("dghjdhdf", "gI Ml");

   m1.put("mapValue", (HashMap) map);

  VelocityEngine velocityEngine = VelocityEngineFactory.getVelocityEngine();
  VelocityContext context = new VelocityContext();
  context.put("img",model);
  context.put("mapReference",m1);
  context.put("iterator", new IteratorTool());


  Template t = velocityEngine.getTemplate("tocTemplate.vm");
  StringWriter writer = new StringWriter();
    t.merge(context , writer);
    System.out.println(writer);
Map Map=newhashmap();
Map m1=新的HashMap();
\\要在模板文件中打印的值
地图放置(“sdfhfg”、“Df lm”);
map.put(“chdgfhd”、“gBc Jk”);
地图放置(“DGHJDDF”、“gI Ml”);
m1.put(“mapValue”,(HashMap)map);
VelocityEngine VelocityEngine=VelocityEngineFactory.getVelocityEngine();
VelocityContext上下文=新的VelocityContext();
上下文。put(“img”,model);
上下文。put(“mapReference”,m1);
put(“迭代器”,新的迭代器工具());
Template t=velocityEngine.getTemplate(“tocTemplate.vm”);
StringWriter编写器=新的StringWriter();
t、 合并(上下文、作者);
System.out.println(writer);
输出为:

 <div>
   <a href="#dghjdhdf">gI Ml</a><br/>
   <a href="#chdgfhd">gBc Jk</a><br/>
   <a href="#sdfhfg">Df lm</a><br/>
 </div>





为什么要对这些值进行排序?我想按原样打印地图。

HasMap的顺序不会随时间保持不变,从:

此类不保证地图的顺序;在里面 特别是,它不能保证订单保持不变 随着时间的推移

为了保持顺序,可以考虑使用如下建议:

这个链表定义了迭代顺序,这通常是 将关键点插入地图的顺序

不是吗