Sure,
This is for the crud pages. They all have the same functions : listAll, newForm, updateForm...and everythings like that for every table of my database. The code of all this functions is almost the same, except that it is type specific. The idea is to code those functions once and make it generic so that I minimise code copy/past. The problem is, you can't make generic static function. One of the workaround explain
here is to create non static functions in a generic class and instantiate it statically in a controller.
Here is a code example. First my crud class :
public class CrudController<Mdl extends Model>extends Controller
{
private final Model.Finder<String, Mdl> find;
private final Form<Mdl> form;
public CrudController(Model.Finder<String, Mdl> find, Form<Mdl> form)
{
this.find = find;
this.form = form;
}
public Result create()
{
Form<Mdl> createForm = form.bindFromRequest();
if (createForm.hasErrors()) {
createForm.get().save();
return badrequest("Error.");
}
return ok("it's great !");
}
[...]
}
Now I create a static instance of that class in a Controller class :
public class Application extends Controller
{
public final static crud.CrudController<Company> crud = new
crud.CrudController<Company>(Company.find, Form.form(Company.class));
[...]
}
So that I could create the following route in my conf/routes file :
GET /test/ controllers.Application.crud.create()
Because the generic class has play actions (functions which return a
Result), this class must extend Controller. The problem is, when a class
extending Controller have a member attribute extending Controller
itself, I get the following error message :