2

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));
}

and this is the tempate code:

@(scripts: List[String])

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

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

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

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!

Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • possible duplicate of [Iterating over Java collections in Scala](http://stackoverflow.com/questions/495741/iterating-over-java-collections-in-scala) – om-nom-nom Jan 29 '14 at 16:20
  • @om-nom-nom my question is in the context of Play! framework, the other one isn't... As I wrote, I'd like to avoid using the converters if there's a play! way of doing things. Because of that I do not think it's a duplicate. – Nitzan Tomer Jan 29 '14 at 16:24
  • @om-nom-nom It is different. Here he wishes to access Scala collection from Java and not vice-versa as mentioned in the link – Jatin Jan 29 '14 at 16:26
  • @Jatin he has Scala list on the receiver end (no matter which language) and tries to push java list into it. I don't see how it is different from trying to iterate java list in scala-like way (and even if he has the opposite situation it is still, very likely, a duplicate) – om-nom-nom Jan 29 '14 at 19:26
  • Could you use `@(scripts: java.util.List[String])` and convert it there if needed? It should just be `scripts.asScala.toList` after importing `JavaConverters._`. – Alexey Romanov Jan 30 '14 at 11:54

2 Answers2

4

Try below:

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));
}
Jatin
  • 31,116
  • 15
  • 98
  • 163
  • Thanks! There's no `toList` method, there's `asScala` but it returns `Buffer` – Nitzan Tomer Jan 29 '14 at 16:17
  • 1
    Oh. never mind, I just need to use both... `JavaConverters.asScalaBufferConverter(scripts).asScala().toList()` – Nitzan Tomer Jan 29 '14 at 16:20
  • @NitzanTomer Sorry it was a mistake. It is `asScala().toList()`. This will work – Jatin Jan 29 '14 at 16:20
  • Thanks. I'll give it a bit more time since I'd prefer to avoid using `JavaConverters` if there's a better approach in play!. If nothing better presents itself I'll mark your answer as the right one. – Nitzan Tomer Jan 29 '14 at 16:26
2

try below in your template code :

@import java.util   
@(scripts: util.List[String])
saral
  • 71
  • 3