Not able to read Object values in Freemarker Template

大兔子大兔子 提交于 2019-12-24 00:36:16

问题


I am naot able to read the scala/java object values in Freemarker Templet

I tried with this:

    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)
    }

and my template is-

<#list mails as std>

    WorkOrderNo:${std.workOrderNo}
    woType:${std.woType}

</#l

i am strugling with this ERROR-

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):

Can anybody help me with this problem


回答1:


Scala doesn't generate a getter method for workOrderNo (nor BeanInfo), so it's not a JavaBean property. It generates a String workOrderNo() method though (my Scala is rusty, but the error message also suggests that). So in the template you had to write std.workOrderNo(). Or, you should customize the ObjectWrapper to recognize String workOrderNo() as a property, via setMethodAppearanceFineTuner(MethodAppearanceFineTuner). If you will work with Scala much, certainly that's what you should do.



来源:https://stackoverflow.com/questions/32201528/not-able-to-read-object-values-in-freemarker-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!