Map[Int, String] in Play framework template

痴心易碎 提交于 2019-12-24 05:53:15

问题


I'm using the Play 2.0.4 framework and I can successfully create a template that accepts a Map[String, String] template by putting this in the controller part:

Map<String, String> test = new HashMap<String, String>();
return ok(views.html.template.render(test));

and in template.scala.html:

@(map : Map[String, String])

However, if I change the first String to an Int (Integer in the controller part), I get this exception:

 error: method render in class template cannot be applied to given types; 

Is it possible to define an Integer -> String mapping in the Play framework and if so, how can I achieve it?

Edit:
The exception occurs when I change the code in the controller to:

Map<Integer, String> test = new HashMap<Integer, String>();

And in the template to:

@(map: Map[Int, String])

回答1:


In your template, you specify that your Map's keys are scala.Ints, but you give it a Map with java.lang.Integers as keys.

The solution is to change the line in your template to

@(map: Map[Integer, String])



回答2:


With your code, the generated render method accepts a Map<Object, String>. Then, you can supply such a map.

There are automatic conversion helpers from Java to Scala (and from Scala to Java). I don't know if one could apply if this case to allow you having a Map<Integer, String> in the render method.



来源:https://stackoverflow.com/questions/15901587/mapint-string-in-play-framework-template

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