I am a newbie to play and have started to develop an application to fetch the data from database.
I have the following code in my application:
public static Result list() {
List products = Productslist.getListOfProducts();
return ok(index.render(products));
}
and this gives me the following error:
Actual List cannot be converted to String on method invocation conversion
You can also view my index.scala.html
#{extends 'main.html' /}
#{set title:'Cars in the car lot' /}
<h1>Products in Lot</h1>
<table border=1>
<tr>
<td>productname</td>
<td>Quantity</td>
<td>Price</td>
</tr>
#{list items:products, as:'product'}
<tr>
<td>${product.getProductname()}</td>
<td>${product.getQuantity()}</td>
<td>${product.getPrice()}</td>
</tr>
#{/list}
</table>
The complete code for Application.java is:
package controllers;
import play.*;
import play.mvc.*;
import models.Productslist;
import views.html.*;
import views.*;
import java.util.*;
public class Application extends Controller
{
public static Result list()
{
List products = Productslist.getListOfProducts();
return ok(index.render(products));
}
}
Can anyone help me find the source of the error?
Looks like error is happening on index.render(products) ; where method render is expecting a string but we are passing list. Can you put in the code of method render()
Look like you used the wrong code for the view. Try this instead
@(products: List[Productslist])
@import helper._
@main("Product list") {
<h1>@product.size() product(s)</h1>
<ul>
@for(product <- products) {
<li>
@product.name
</li>
}
</ul>
}
Use play clean-all
before running your server
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.