1

我正在使用带有 freemarker 的 java 通过 FTL(模板文件)和 XML 生成 HTML 文件。我在多个文件中得到了结果,但每个文件都包含整个结果。我希望每个文件都包含自己的结果。为了给你更多的细节,看看我的java代码的这一部分:(解决方案应该很简单,但我找不到)

static void freemarkerDo(Map datamodel, String template) throws Exception{
  try {
      File file = new File("Avis.xml");
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      doc.getDocumentElement().normalize();
      NodeList nodeLst = doc.getElementsByTagName("Avis");

      Configuration cfg = new Configuration();

      Template tpl = cfg.getTemplate(template);



      for (int s = 0; s < nodeLst.getLength(); s++) {

        Node fstNode = nodeLst.item(s);

        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

            Element fstElmnt = (Element) fstNode;
            NodeList flNmElmntLst = fstElmnt.getElementsByTagName("Filename");
            Element flNmElmnt = (Element) flNmElmntLst.item(0);
            NodeList flNm = flNmElmnt.getChildNodes();
            FileWriter writer = new FileWriter(((Node) flNm.item(0)).getNodeValue()+".html");

            try {
                tpl.process(datamodel, writer);
                }
            finally{
                writer.close();
                    }
                                                        }



      }
      } catch (Exception e) {
        e.printStackTrace();
      }

}

谢谢你的帮助。

4

1 回答 1

1

我不知道此方法的作用或数据模型的设置位置,但是在我看来,您正在传递整个数据模型,因此这可以解释为什么您在每个文件中都有整个数据模型。

tpl.process(datamodel, writer); // does what, with what?

当你调试你的代码时你看到了什么?

于 2010-12-06T18:38:12.867 回答