1

我只是想将我的表单传递给我的控制器,无论我尝试什么,我都会收到此错误:

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

并将我的站点更改addServicereturn TODO;可以正常编译的站点,并且可以很好地完成整个过程。即使我仍在返回 TODO,此行也会中断站点:

private static final Form<Service> sServiceForm = Form.form(Service.class);
4

1 回答 1

1

如果您希望它使用 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"/>

  }

}
于 2016-03-20T04:49:46.430 回答