構成ファイル:application.conf
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"
コントローラクラス=>Application.java
package controllers;
import play.*;
import play.mvc.*;
import models.City;
import play.data.Form;
import views.html.*;
public class Application extends Controller {
final static Form<City> cityForm = form(City.class);
public static Result index() {
City pune=new City();
pune.name="pune";
pune.save();
City mumbai=new City();
mumbai.name="mumbai";
mumbai.save();
City city=City.get(2L);
return ok(index.render(cityForm.fill(city),City.all()));
}
}
モデルクラス=>City.java:
package models;
import javax.persistence.Entity;
import javax.persistence.Id;
import play.db.ebean.Model;
import java.util.List;
import com.avaje.ebean.validation.NotNull;
@Entity
public class City extends Model{
@Id
public Long id;
@NotNull
public String name;
public static Finder<Long, City> find = new Finder(Long.class, City.class);
public static City get(Long id){
return find.byId(id);
}
public static List<City> all() {
return find.all();
}
}
テンプレートファイル=>index.scala.html
@(cityForm: Form[City],cities: List[City])
<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<div>
<select>
@for(city <- cities){
<option value="@city.id" @{if(city.id.toString().equals(cityForm("id").value)) "selected='selected'"}/>@city.name</option>
}
</select>
</div>
</body>
</html>