1

我是 freemarker 的初学者,我想用它来生成一些重复的代码。

从这样一个简单的类:

public class Point {
  private Integer x;
  private Integer y;
  private String name;
}

对于每个属性,我需要生成如下行:

ValueProvider<Point,Integer> x();
ValueProvider<Point,Integer> y();
ValueProvider<Point,String> name();

为此,我有这个简单的模板:

ValueProvider<${clazz},${attrType}> ${attrName}();

然后,我想生成一个像这样的完整类:

public final class PointValueProviders {

  public interface PointPropertyAccess extends PropertyAccess<Point>{
    ValueProvider<Point,Integer> x();
    ValueProvider<Point,Integer> y();
    ValueProvider<Point,String> name();
  }

  public static final PointPropertyAccess POINT_PA= GWT.create(PointPropertyAccess.class);

  private PointValueProviders(){}

};

为此,我有一个问题:我不知道如何在像这样的更大模板中应用小模板的次数不确定:

public final ${clazz}ValueProviders {

  public interface ${clazz}PropertyAccess extends PropertyAccess<${clazz}>{

  //Here, How do I tell freemarker to use the small template???

  //ValueProvider<${clazz},${attrType}> ${attrName}();
  //ValueProvider<${clazz},${attrType}> ${attrName}();
  //ValueProvider<${clazz},${attrType}> ${attrName}();
  //ValueProvider<${clazz},${attrType}> ${attrName}();
  //etc..

  }

  public static final ${clazz}PropertyAccess ${clazzUpperCase}_PA= GWT.create(${clazz}PropertyAccess.class);

  private ${clazz}ValueProviders(){}

};

任何想法?

4

1 回答 1

1

模板将显示您提供给他们的一些数据。所以重要的问题是,模板如何知道要输出什么class//三重奏attrTypeattrName你应该提供一个列表,让我们称之为它props,然后循环遍历它

<#list props as prop>
   ValueProvider<${prop.clazz},${prop.attrType}> ${prop.attrName}();
</#list>

否则,要创建小型可重用模板,请使用#macro(这个更灵活)或#include. 在FreeMarker 手册中查看它们。

于 2013-05-27T09:09:20.020 回答