我只是想給我的形式傳遞給我的控制器,我不管收到此錯誤是我嘗試:調用靜態方法,並把型號爲控制器
render(play.api.data.Form<models.Service>) in 'null' cannot be applied to (play.data.Form<models.Service)
錯誤行:
return ok(info.render(sServiceForm));
Info.scale.html - 查看
@(serviceForm : Form[Service])
@import helper._
@main("Service info") {
<h1>Service Information</h1>
@helper.form(action = routes.Services.save()) {
<fieldset>
<legend>Service</legend>
@helper.inputText(serviceForm.field("code"), '_label -> "Code")
@helper.inputText(serviceForm.field("description"), '_label -> "Description")
@helper.inputText(serviceForm.field("description"), '_label -> "Description")
</fieldset>
<input type="submit" value="Save" />
}
}
Service.java - 型號
package models;
import com.avaje.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Created by James on 3/4/2016.
*/
// Telling play framework that this is a class thats going to map as a model to save service records
@Entity
public class Service extends Model {
// Internal ID to reference a certain activity
@Id
public String code;
public String description;
}
Services.java - 控制器
package controllers;
import models.Service;
import play.mvc.Controller;
import play.mvc.Result;
import play.data.Form;
import views.html.services.info;
/**
* Created by James on 3/4/2016.
*/
public class Services extends Controller {
// Creating static class variable, calling static method and passing our model class.
//private static final Form<Service> sServiceForm = Form.form(Service.class);
private static final Form<Service> sServiceForm = play.data.Form.form(Service.class);
public Result list() {
return TODO;
}
public Result addService() {
return ok(info.render(sServiceForm));
}
public Result save()
{
return TODO;
}
}
如果我註釋掉:
private static final Form<Service> sServiceForm = Form.form(Service.class);
而改變我的addService
到return TODO;
網站編譯良好,我可以遍歷很好。即使我仍然返回TODO,該行會破壞網站:
private static final Form<Service> sServiceForm = Form.form(Service.class);
在哪裏出現了這個錯誤?在IDEA或在您的控制檯?我認爲這只是一個IDEA「問題」,它不會識別'play.data.Form'(你的控制器上的Java表單版本)和'play.api.data.Form'(Scala,在你看來)之間的隱式轉換, 。讓IDEA開心的一種可能方式是像'@(serviceForm:play.data.Form [Service])'聲明你的視圖。另外,如果您已經在使用Play 2.5.0,請注入一個「FormFactory」而不是使用已棄用的Form.form。 – marcospereira