从 FreeMarker 模板访问 Java 嵌套 类
Accessing Java nested classes from FreeMarker templates
public class MyModel {
private final com.google.gson.Gson gson = new Gson();
private final SomeData someData = new SomeData();
private class SomeData extends JsonModel() {
String text = "text";
}
private class JsonModel() {
public String toJson() {
return gson.toJson(this);
}
}
public SomeData getSomeData() {
return someData;
}
}
假设 MyModel
在 FTL 中可以作为 myModel
访问,为什么 myModel.someData.toJson
或 myModel.getSomeData().toJson()
会因 The following has evaluated to null or missing: myModel.someData.toJson
或 The following has evaluated to null or missing: myModel.getSomeData().toJson()
而失败?
JsonModel
必须是 public,否则其成员不会被暴露。
public class MyModel {
private final com.google.gson.Gson gson = new Gson();
private final SomeData someData = new SomeData();
private class SomeData extends JsonModel() {
String text = "text";
}
private class JsonModel() {
public String toJson() {
return gson.toJson(this);
}
}
public SomeData getSomeData() {
return someData;
}
}
假设 MyModel
在 FTL 中可以作为 myModel
访问,为什么 myModel.someData.toJson
或 myModel.getSomeData().toJson()
会因 The following has evaluated to null or missing: myModel.someData.toJson
或 The following has evaluated to null or missing: myModel.getSomeData().toJson()
而失败?
JsonModel
必须是 public,否则其成员不会被暴露。