简体   繁体   中英

How to render a list/stringbuilder to template

I have a list of objects that i retrieved from a JSon File. I am trying to render them into the index.scala.html to create a table, but I am unable to do so. Help?

I tried @(StringBuilder: mystring) and it did not work.

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) .

If instead you want to pass the StringBuilder to the template, simply avoid calling toString and bind the variable like this @(mystring: StringBuilder) .

For List s, simply bind the variable in the template @(integers: List[Int]) and then use map

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

or simpler @for function:

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

More info and also some examples are also on the Play documentation for Java .

Firstly, To receive the data in template, data type should always be declared after creating the variable. It should be @(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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