faisal abdulai
faisal abdulai

Reputation: 3819

render list of objects in play 2 framework with java

i am working on project using play 2 framework with java. I want to populate a drop down list from a database table. I have this code that gets a list of the items from the database. the code snippet is shown below.

public static Result gestureNames()
  {
  List <GestureClassEntity> gcet = GestureClassEntity.find.all();
  return ok(render(gcet)); 
}

however when i run this code play framework tells me that it cannot find render.

i have tried to modify the code which i have shown below

public static void gestureNames()
  {
  List <GestureClassEntity> gcet = GestureClassEntity.find.all();
  render(gcet); 
}

play tells me again that it cannot use a method returning Unit as an Handler

still struggling to understand the play framework can some kindly help me out. Cos i am working on a project and time is running out.

Upvotes: 0

Views: 6820

Answers (1)

biesior
biesior

Reputation: 55798

Remember previous question? https://stackoverflow.com/a/12180812/1066240

render() is method of the view, so to use it, you need to specify the view

public static Result gestureNames(){
    List <GestureClassEntity> gcet = GestureClassEntity.find.all();
    return ok(views.html.gestures.render(gcet));    
}

app/views/gestures.scala.html

@(gesturesListFromMyController: List[GestureClassEntity])

@for(gesture <- gesturesListFromMyController){
    <div class="gesture-item">
        <h2>@gesture.name</h2>
        <p>@gesture.description</p>
    </div>
}

BTW: try to simplify your enigmatic models' names, your life will be better. Can't be GestureClassEntity named just as Gesture ???

Upvotes: 6

Related Questions