玩!无法将java列表转换为scala列表 [英] Play! failing to convert java list to scala list

查看:436
本文介绍了玩!无法将java列表转换为scala列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的java控制器中的字符串列表传递给scala模板。

这是查看代码:

I'm trying to pass a list of strings from my java controller to the scala template.
This is the view code:

public static Result index() {
    List<String> scripts = Arrays.asList(
        "script1.js",
        ...
        "scriptN.js"
    );

    return ok(views.html.index.render(scripts));
}

这是诱惑代码:

@(scripts: List[String])

@main("test page")(scripts) {
    ... html here ...
}

我得到的错误(在Typesafe中)激活编译页面):

The error I'm getting (in the Typesafe Activation Compile page):


类索引中的方法呈现不能应用于给定类型;
需要
:scala .collection.immutable.List

found:java.util.List

reason:实际参数java.util.List不能转换为scala.collection.immutable.List by
方法调用转换

method render in class index cannot be applied to given types;
required: scala.collection.immutable.List
found: java.util.List
reason: actual argument java.util.List cannot be converted to scala.collection.immutable.List by method invocation conversion

有没有办法解决它而不使用java> scala转换?

我发现了这个问题: Play不转换java-list scala-list 描述了类似的情况,虽然我没有任何templateImports我知道,我甚至没有看到Build.scala文件...

Is there a way to solve it without using the java > scala conversions?
I found this question: Play doesn't convert java-list to scala-list which describes a similar situation, though I do not have any templateImports that I'm aware of, I don't even see a Build.scala file...

任何想法?
谢谢!

Any ideas? Thanks!

推荐答案

请尝试以下操作:

import scala.collection.JavaConverters;

public static Result index() {
    List<String> scripts = Arrays.asList(
        "script1.js",
        ...
        "scriptN.js"
    );
    scala.collection.immutable.List<String> ls = JavaConverters.asScalaBufferConverter(scripts).asScala().toList();
    return ok(views.html.index.render(ls));
}

这篇关于玩!无法将java列表转换为scala列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆