处理模板中的重复值

时间:2013-02-13 10:20:43

标签: java forms scala playframework-2.1

文档说我可以将@repeat用于表单中定义的列表。

http://www.playframework.com/documentation/2.1.0/JavaFormHelpers页面底部。

这可能是一个完全愚蠢的问题......我可以使用类似的地图吗?

现在我有一个辅助类,包括String key和String value。它有效,但我在模板中有一些逻辑。在我看来这不好......

修改:更多信息

假设我有

class Article {
  ...
  Map<String, String> resources;
  ...getters, setters...
}

我调用view来处理表单

return ok(form.render(Form.form(Article.class)));

in form.scala.html

@for((key, value) <-formArt("resources")) { 
    @key, @value
}

给我错误:

value map is not a member of play.data.Form.Field 

这样就完全了,因为它不再是map,而是formField。 scala中有帮助器来处理List,但我不知道如何帮助处理Map。 (如果我尝试类似的东西,例如使用@repeat helper,给我同样的错误)

对于那些询问Field.value中的内容的人

{value1=key1, value2=key2 ...}

1 个答案:

答案 0 :(得分:0)

如果您只想迭代地图,可以使用@defining()@for()执行此操作:

public static Map<String, Object> myMap() {
    Map<String, Object> myMap = new HashMap<>();
    myMap.put("name", "John");
    myMap.put("secondName", "Doe");
    myMap.put("age", 23);
    return myMap;
}

视图:

@defining(Application.myMap()) { myMap =>

    Hello @myMap.get("name") @myMap.get("secondName")!
    <br><br>

    All entries of the map: <br>
    @for(entry <- myMap.entrySet()){
       Field <b>@entry.getKey()</b> has value: @entry.getValue() <br>
    }

}