我只是想将我的表单传递给我的 Controller ,无论我尝试什么,我都会收到此错误:
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 - Controller
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);
最佳答案
如果您希望它使用 FormFactory 工作。 您可以按照此代码操作。 来自 gitter 的人帮助了我。 所以,这实际上是他的功劳。
这是我的代码:
服务.java
package controllers;
import models.Service;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.services.info;
import javax.inject.Inject;
public class Services extends Controller {
private final Form<Service> serviceForm;
@Inject
public Services(FormFactory formFactory) {
this.serviceForm = formFactory.form(Service.class);
}
public Result list() {
return TODO;
}
public Result addService() {
return ok(info.render(serviceForm));
}
public Result save() {
return TODO;
}
}
和info.scala.html
@(serviceForm : play.data.Form[Service])
@import helper._
@main("Service info"){
<h1>Service Information</h1>
@form(action = routes.Services.save()) {
<fieldset>
<legend>Service</legend>
@inputText(serviceForm("code"), '_label -> "Code")
@inputText(serviceForm("description"), '_label -> "Description")
@inputText(serviceForm("description"), '_label -> "Description")
</fieldset>
<input type="submit" value="Save"/>
}
}
关于java - 调用静态方法并将模型传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807850/