2012-07-27 66 views
0

我正在阅读“Play for Java”一书,并尝试使用示例代码。现在,我就死在了一个问题:通过运行此示例代码Playframework:方法ok(内容)不适用于列表类型参数?

import ... 

public class Application extends Controller { 

    public static Result index() { 

    ... 
    ... 

     List<StockItem> items = StockItem.find() 
       .findList(); 
     return ok(items); 

    } 

} 

ECLIPSE返回的错误消息“的方法确定(内容)的类型的结果是不适用的参数(列表)”。

有人知道我该如何解决它吗?感谢您的时间。

回答

3

这取决于你想要返回什么样的数据格式(JSON,XML等)。 实施例示出了JSON结果:https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/java/play/mvc/Results.java

或Javadoc中:

import ... 

public class Application extends Controller { 

    public static Result index() { 
    List<StockItem> items = StockItem.find().findList(); 
    return ok(Json.toJson(items)); 
    } 

} 

“OK” 方法可以从结果类的源代码视图的所有变型http://www.playframework.org/documentation/api/2.0.2/java/play/mvc/Results.html

3

ok()接受StringJSON(如武装写道),File甚至InputStream不是Listcheck in the code

最有可能要返回渲染view代替:

import views.html.yourview; 

public class Application extends Controller { 

    public static Result index() { 
    List<StockItem> items = StockItem.find().findList(); 
    return ok(yourview.render(items)); 
    } 

} 

/app/views/yourview.scala.html

@(items: List[StockItem]) 

<ul> 
    @for(item <- items){ 
    <li>@item.title</li> 
    } 
</ul> 
相关问题