Play框架模板中的Map [Int,String] [英] Map[Int, String] in Play framework template
问题描述
我正在使用Play 2.0.4框架,并且可以通过将其放置在控制器部分中来成功创建一个接受Map[String, String]
模板的模板:
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));
和template.scala.html
中的
@(map : Map[String, String])
但是,如果将第一个String
更改为Int
(控制器部分为Integer),则会出现此异常:
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;
是否可以在Play框架中定义一个Integer-> String映射,如果可以,该如何实现?
Is it possible to define an Integer -> String mapping in the Play framework and if so, how can I achieve it?
修改:
当我将控制器中的代码更改为:
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])
推荐答案
在模板中,您指定Map
的键是scala.Int
s,但是给它一个Map
并带有java.lang.Integer
s作为键.
In your template, you specify that your Map
's keys are scala.Int
s, but you give it a Map
with java.lang.Integer
s as keys.
解决方案是将模板中的行更改为
The solution is to change the line in your template to
@(map: Map[Integer, String])
这篇关于Play框架模板中的Map [Int,String]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!