【问题标题】:Play Framework: incompatible types: java.util.List<models.Vehicle> cannot be converted to java.lang.StringPlay Framework:不兼容的类型:java.util.List<models.Vehicle> 无法转换为 java.lang.String
【发布时间】:2016-05-22 23:31:22
【问题描述】:

我正在 Play framework 2.5 中尝试一个简单的视图,但我不断收到此编译时错误 java.util.List&lt;models.Vehicle&gt; cannot be converted to java.lang.String

我已经试过this answer

scala.collection.immutable.List<String> ls = JavaConverters.asScalaBufferConverter(scripts).asScala().toList(); 

但没有.toList() 功能,无法识别。 这是我的代码:

应用程序.java

package controllers;

import models.Vehicle;
import play.mvc.Controller;
import play.mvc.Result;
import scala.collection.JavaConverters;

public class Application extends Controller {

    public Result index() {
    //        this does not work
    //        scala.collection.immutable.List<Vehicle> ls = JavaConverters.asScalaBufferConverter(Vehicle.finder.all()).asScala().toList();
              return ok(views.html.index.render(Vehicle.finder.all())); //here is the error
    }

}

index.scala.html

@(vehicles: java.util.List[Vehicle])

@main("Welcome to Play") {

    <header>
        <hgroup>
            <h1>Dashboard</h1>
            <h2>Vehicles</h2>
        </hgroup>
    </header>

    <ul>
    @for(vehicle <- vehicles) {
        <li>@vehicle.getModel</li>
    }
    </ul>
}

Vehicle.java 中的相关代码

package models;

import javax.persistence.*;
import java.util.List;
@Entity
public class Vehicle extends BaseEntity {

    public static Finder<Long, Vehicle> finder = new Finder<Long, Vehicle>(Vehicle.class){};
    @Column(nullable = false)
    String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 哦,我知道了,只需要在项目目录中做clean compile...我想我只是累了

标签: java scala playframework playframework-2.5


【解决方案1】:

好的,万一以后有人发现这个,试试

activator clean compile

在您的项目文件夹中。

只做compile 根本没有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 2017-03-18
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多