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どのように知るかということattrTypeです。attrNameそれらのリストを提供する必要があります。それpropsを と呼びましょう。

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

それ以外の場合は、再利用可能な小さなテンプレートを作成するには#macro(こちらの方が柔軟性があります) または を使用し#includeます。FreeMarker マニュアルでそれらを参照してください。

于 2013-05-27T09:09:20.020 に答える