I’m doing some tests with a playframework2 app and I have noticed that if I do not put
my objects in the models folder I’m not allowed to use them in my view.
e.g Let say I put a class SimpleObject in a package name simple.
SimpleObject.class:
public class SimpleObject {
public SimpleObject(){}
public SimpleObject(String name){
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In my Application controller
i render the view with a new SimpleObject:
public static Result doSomeAction() {
SimpleObject simpleObject = new SimpleObject("test");
return ok(test.render(simpleObject));
}
In my test.scala.html:
@(simpleObject: SimpleObject)
@main("Here is the result:") {
@{simpleObject.getName()}
}
This results in an error:
not found: type SimpleObject
If I move SimpleObject to the package models it compiles and works.
In my other application I have implemented the Elasticsearch module. There I receive
an IndexResult that I want to loop over in my view. I get the same error as with the SimpleObject.
So my questions here is sort of architectural. Whats the “play 2 way” of handling this?
Should I implement a view object and a builder, throw the IndexResult object in and create
a view object?
Or is there a way of telling my app to scan other packages too?
Maybe there is some other way of thinking?
You could try using an import in the view, ie:
Or you could try organizing your models/classes in sub-folders/packages of models.