Model 帮助无法在play framework controller中调用非静态方法模型?
我一直在遵循PlayFramework1.1.1Yabe添加评论教程,但最终遇到了这个问题 问题是出现了此错误消息 编译错误 无法编译文件/app/controllers/Application.java。引发的错误是:无法从类型Permainan对非静态方法addComment(String、String、boolean、String)进行静态引用 这是我的Application.java控制器内容Model 帮助无法在play framework controller中调用非静态方法模型?,model,controller,playframework,non-static,Model,Controller,Playframework,Non Static,我一直在遵循PlayFramework1.1.1Yabe添加评论教程,但最终遇到了这个问题 问题是出现了此错误消息 编译错误 无法编译文件/app/controllers/Application.java。引发的错误是:无法从类型Permainan对非静态方法addComment(String、String、boolean、String)进行静态引用 这是我的Application.java控制器内容 package controllers; import play.*; import pl
package controllers;
import play.*;
import play.mvc.*;
import play.libs.*;
import java.util.*;
import models.*;
public class Application extends Controller {
public static void permainanKomentar(Long permainanId, String author, String email, boolean showEmail, String content) {
Permainan permainan = Permainan.findById(permainanId);
Permainan.addComment(author, email, showEmail, content); >> (this line)
show(permainanId);
}
}
对于Permainan.java模型
package models;
import java.util.*;
import javax.persistence.*;
import play.data.binding.*;
import play.db.jpa.*;
import play.data.validation.*;
@Entity
public class Permainan extends Model {
@Required
public String nama;
@Required
@MaxSize(5000)
@Lob
public String deskripsi;
@MaxSize(2000)
@Lob
public String material;
@MaxSize(4000)
@Lob
public String syair;
public Date postedAt;
@OneToMany(mappedBy="permainan", cascade=CascadeType.ALL)
public List<Komentar> komentar;
@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> tags;
public Permainan(String nama, String deskripsi, String material, String syair) {
this.komentar = new ArrayList<Komentar>();
this.tags = new TreeSet<Tag>();
this.nama = nama;
this.deskripsi = deskripsi;
this.material = material;
this.syair = syair;
this.postedAt = new Date();
}
public Permainan addComment(String author, String email, boolean showEmail, String content) {
Komentar newKomentar = new Komentar(this, author, email, showEmail, content).save();
this.komentar.add(newKomentar);
this.save();
return this;
}
}
封装模型;
导入java.util.*;
导入javax.persistence.*;
导入play.data.binding.*;
导入play.db.jpa.*;
导入play.data.validation.*;
@实体
公共类Permainan扩展模型{
@必需的
公共字符串nama;
@必需的
@最大尺寸(5000)
@高球
公共字符串;
@MaxSize(2000)
@高球
公共字符串材料;
@最大尺寸(4000)
@高球
公共字符串syair;
发布日期:postedAt;
@OneToMany(mappedBy=“permainan”,cascade=CascadeType.ALL)
公共名单科门塔尔;
@ManyToMany(cascade=CascadeType.PERSIST)
公共设置标签;
公共Permainan(字符串nama、字符串deskripsi、字符串材质、字符串syair){
this.komentar=新的ArrayList();
this.tags=新树集();
this.nama=nama;
this.deskripsi=deskripsi;
这个材料=材料;
this.syair=syair;
this.postedAt=新日期();
}
公共Permainan addComment(字符串作者、字符串电子邮件、布尔showEmail、字符串内容){
Komentar newKomentar=新的Komentar(此、作者、电子邮件、showEmail、内容)。保存();
本.komentar.add(newKomentar);
这是save();
归还这个;
}
}
Java是一种区分大小写的语言,这意味着您必须小心使用的单词的大小写。在您的示例中:
Permainan-Permainan=Permainan.findById(permainanId)
在这里,您定义了类Permainan的一个实例,称为Permainan(请注意,对于类和实例,您使用的是相同的实例,在大小写上略有不同)
添加评论(作者、电子邮件、showEmail、内容)
这里使用的是Permainan类,而不是实例;对于您的对象,没有称为addComment的静态方法
所以我认为你应该使用:
添加评论(作者、电子邮件、showEmail、内容)
大家好,这是我的错误。添加评论(作者、电子邮件、showEmail、内容);>>(此行)应改为permainan.addComment(作者、电子邮件、showEmail、内容);>>(此行)