Not able to read Object values in Freemarker Templ

2019-07-17 08:25发布

问题:

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.