无法读取 Freemarker 模板中的对象值
Not able to read Object values in Freemarker Template
我无法读取 Freemarker Templet 中的 scala/java 对象值
我试过这个:
case class ScheduleEmail(workOrderNo:String,
name:String,
woType:String,
numberOfAssets:String,
artisan:String,
dueDate:Date,priority:String)
object ScheduleMailSending extends App{
val scheduleEmail1= List(ScheduleEmail("1", "Oil Change1", "WO", "3", "XYZ", Date.valueOf("2015-01-01"), "High"))
val configaration = new Configuration
configaration.setClassForTemplateLoading(this.getClass, "/")
configaration.setObjectWrapper(new DefaultObjectWrapper())
val data: util.HashMap[String, Object] = new util.HashMap[String, Object]
val templet = configaration.getTemplate("schedule.ftl")
data.put("mails",scheduleEmail1.asJava)
val writer =new StringWriter()
templet.process(data,writer)
println("writer"+writer)
}
我的模板是-
<#list mails as std>
WorkOrderNo:${std.workOrderNo}
woType:${std.woType}
</#l
我正在为这个错误而苦恼-
SEVERE: Error executing FreeMarker template
FreeMarker template error:
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+method (wrapper: f.e.b.SimpleMethodModel):
==> std.workOrderNo [in template "schedule.ftl" at line 2, column 15]
The failing instruction (FTL stack trace):
----------
==> ${std.workOrderNo} [in template "schedule.ftl" at line 2, column 13]
----------
Java stack trace (for programmers):
谁能帮我解决这个问题
Scala 不会为 workOrderNo
(BeanInfo
)生成 getter 方法,因此它不是 JavaBean 属性。它虽然生成了一个 String workOrderNo()
方法(我的 Scala 生锈了,但错误消息也表明了这一点)。所以在模板中你必须写 std.workOrderNo()
。或者,您应该通过 setMethodAppearanceFineTuner(MethodAppearanceFineTuner)
自定义 ObjectWrapper
以将 String workOrderNo()
识别为 属性。如果您经常使用 Scala,那当然是您应该做的。
我无法读取 Freemarker Templet 中的 scala/java 对象值
我试过这个:
case class ScheduleEmail(workOrderNo:String,
name:String,
woType:String,
numberOfAssets:String,
artisan:String,
dueDate:Date,priority:String)
object ScheduleMailSending extends App{
val scheduleEmail1= List(ScheduleEmail("1", "Oil Change1", "WO", "3", "XYZ", Date.valueOf("2015-01-01"), "High"))
val configaration = new Configuration
configaration.setClassForTemplateLoading(this.getClass, "/")
configaration.setObjectWrapper(new DefaultObjectWrapper())
val data: util.HashMap[String, Object] = new util.HashMap[String, Object]
val templet = configaration.getTemplate("schedule.ftl")
data.put("mails",scheduleEmail1.asJava)
val writer =new StringWriter()
templet.process(data,writer)
println("writer"+writer)
}
我的模板是-
<#list mails as std>
WorkOrderNo:${std.workOrderNo}
woType:${std.woType}
</#l
我正在为这个错误而苦恼-
SEVERE: Error executing FreeMarker template
FreeMarker template error:
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+method (wrapper: f.e.b.SimpleMethodModel):
==> std.workOrderNo [in template "schedule.ftl" at line 2, column 15]
The failing instruction (FTL stack trace):
----------
==> ${std.workOrderNo} [in template "schedule.ftl" at line 2, column 13]
----------
Java stack trace (for programmers):
谁能帮我解决这个问题
Scala 不会为 workOrderNo
(BeanInfo
)生成 getter 方法,因此它不是 JavaBean 属性。它虽然生成了一个 String workOrderNo()
方法(我的 Scala 生锈了,但错误消息也表明了这一点)。所以在模板中你必须写 std.workOrderNo()
。或者,您应该通过 setMethodAppearanceFineTuner(MethodAppearanceFineTuner)
自定义 ObjectWrapper
以将 String workOrderNo()
识别为 属性。如果您经常使用 Scala,那当然是您应该做的。