简体   繁体   English

如何将列表/字符串生成器呈现到模板

[英]How to render a list/stringbuilder to template

I have a list of objects that i retrieved from a JSon File. 我有一个从JSon文件中检索到的对象的列表。 I am trying to render them into the index.scala.html to create a table, but I am unable to do so. 我正在尝试将它们渲染到index.scala.html中以创建表,但无法这样做。 Help? 救命?

I tried @(StringBuilder: mystring) and it did not work. 我尝试了@(StringBuilder: mystring) ,但没有成功。

I was wondering after I am successful (hopefully) in rendering the list/stringbuilder into the template how do I use it to make a table? 我想知道在成功(希望)将列表/字符串生成器呈现到模板中之后,如何使用它制作表格吗?

public static Result index() {
  List<MetaModel> arr = getData();

  StringBuilder myString = new StringBuilder();
  for(MetaModel model : arr)
  {
    myString.append(model.toString());
  }

  return ok(index.render(myString.toString()));
}  

My Java is a bit rusty, anyway you are calling toString() on the StringBuilder but in the template you have @(StringBuilder: mystring) which is the wrong type and the wrong syntax, it should be @(myString: String) . 我的Java有点生锈,无论如何,您都在StringBuilder上调用toString() ,但是在模板中,您使用了错误的类型和语法的@(StringBuilder: mystring) ,它应该是@(myString: String)

If instead you want to pass the StringBuilder to the template, simply avoid calling toString and bind the variable like this @(mystring: StringBuilder) . 相反,如果您想将StringBuilder传递给模板,只需避免调用toString并像这样@(mystring: StringBuilder)绑定变量。

For List s, simply bind the variable in the template @(integers: List[Int]) and then use map 对于List ,只需将变量绑定到模板@(integers: List[Int]) ,然后使用map

<ul>
  @integers.map { someInt => <li>@someInt</li> }
</ul>

or simpler @for function: 或更简单的@for函数:

<ul>
   @for( someInt <- integers) { <li>@someInt</li> }
</ul>

More info and also some examples are also on the Play documentation for Java . 有关JavaPlay文档,请参见更多信息和一些示例。

Firstly, To receive the data in template, data type should always be declared after creating the variable. 首先,要接收模板中的数据,应始终在创建变量后声明数据类型。 It should be @(myString : StringBuilder) . 它应该是@(myString:StringBuilder) Secondly, you have send wrong data type to the template. 其次,您向模板发送了错误的数据类型。 you have send the data of type String but you are trying to receive the data of type StringBuilder 您已经发送了String类型的数据,但是您正在尝试接收StringBuilder类型的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM