本文介绍了无法读取Freemarker模板中的对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很能读取Freemarker Templet中的scala/java对象值

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

我尝试过:

    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

我正在为这个错误而苦恼-

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

推荐答案

Scala不会为workOrderNo(也不是BeanInfo)生成getter方法,因此它不是JavaBean属性.但是它会生成一个String workOrderNo()方法(我的Scala生锈了,但错误消息也表明了这一点).因此,您必须在模板中编写std.workOrderNo().或者,您应该自定义ObjectWrapper以通过setMethodAppearanceFineTuner(MethodAppearanceFineTuner)String workOrderNo()识别为属性.如果您将与Scala一起工作,那肯定是您应该做的.

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.

这篇关于无法读取Freemarker模板中的对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 11:40