2016-03-04 14 views
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); 
+0

在哪裏出現了這個錯誤?在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

回答

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

    } 

} 
+0

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](http://stackoverflow.com/questions/ask)來提問。您還可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](http://stackoverflow.com/help/)時吸引更多人關注此問題什麼聲譽)。 - [來自評論](/ review/low-quality-posts/11699871) –

+0

我已經編輯它。這一次,回答我遇到的問題 – fvthree