Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 插入列表项作为属性?_Java_Freemarker - Fatal编程技术网

Java 插入列表项作为属性?

Java 插入列表项作为属性?,java,freemarker,Java,Freemarker,目标:使用Freemarker生成以下形式的Java代码 public void save(){ helper.save(); } public void load(){ helper.load(); } //Other such lifecycle methods 基本上,我有一些“生命周期”方法名称,我想为它们中的每一个生成代码。但是,我只在传入的模型对象需要时生成方法。我的模型类看起来像: public class Model{ private boolean

目标:使用Freemarker生成以下形式的Java代码

public void save(){
    helper.save();
}

public void load(){
    helper.load();
}

//Other such lifecycle methods
基本上,我有一些“生命周期”方法名称,我想为它们中的每一个生成代码。但是,我只在传入的模型对象需要时生成方法。我的模型类看起来像:

public class Model{
    private boolean load;
    private boolean save;
    //bools for other lifecycle methods

    public Model(boolean load, boolean save){
        this.load = load;
        this.save = save;
    }

    public boolean getLoad(){
        return load;
    }

    public boolean getSave(){
        return save;
    }
}
我的freemarker模板:

<#assign methodNames = ["load", "save"]>
<#list methodNames as method>
<#if model.method>
public void ${method}(){
    helper.${method}();
}
</#if>
</#list>
//Obtain a writer object first. Then ...
Model model = new Model(true, false);
myTemplate.process(model, writer);
writer.close();
此操作失败,错误提示我不能将该
放在
中。我还直接尝试了
,但显然不起作用

为完整起见,以下是我调用模板的方式:

<#assign methodNames = ["load", "save"]>
<#list methodNames as method>
<#if model.method>
public void ${method}(){
    helper.${method}();
}
</#if>
</#list>
//Obtain a writer object first. Then ...
Model model = new Model(true, false);
myTemplate.process(model, writer);
writer.close();

因此,问题是-如何让Freemarker从另一个模型对象中给我列表变量的值?

这与其他典型语言中的情况非常相似:
模型[方法]
model.method
实际上是
model[“method”]
的缩写。您还可以编写类似于
model[“foo”+someVar]
,因此正是
someContainer[someKeyExpression]

。我所要做的就是在
if
条件中使用这个语法: