调用静态方法并将模型传递给控制器

时间:2016-03-04 23:20:00

标签: java playframework playframework-2.5

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

class Derived: Base {

private:

    struct MockDerived: Base {
        using Base::Base;

        // Override all pure virtual methods with dummy implementations:
        int f() override; // No definition required
    };

public:

    template <typename ... Args>
    Derived(Args && ... args)
            noexcept(noexcept(MockDerived(std::forward<Args>(args)...)))
        : Base(std::forward<Args>(args)...)
        , m_f(g())
    {}

    int f() override { return 42; } // Real implementation

    int m_f;

};

错误行:

render(play.api.data.Form<models.Service>) in 'null' cannot be applied to (play.data.Form<models.Service)

Info.scale.html - 查看

return ok(info.render(sServiceForm));

Service.java - 模型

@(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" />
  }
}

Services.java - Controller

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

如果我发表评论:

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该网站编译得很好,我可以完成它。即使我还在返回TODO,这条线也会破坏网站:

return TODO;

1 个答案:

答案 0 :(得分:1)

如果您希望它使用FormFactory工作。 您可以遵循此代码。 来自gitter的人帮助了我。 所以,实际上是他的信用。

这是我的代码:

Services.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"/>

  }

}