(Reference) Books you are studying https://www.amazon.co.jp/Play-Framework-2%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-Java%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E3%82%A2%E3%82%B8%E3%83%A3%E3%82%A4%E3%83%ABWeb%E9%96%8B%E7%99%BA-%E6%B4%A5%E8%80%B6%E4%B9%83/dp/4798133922/ref=cm_cr_srp_d_product_top?ie=UTF8
The simplest method is as follows
「routes」
GET /item controllers.Application.setitem()
「Conotrller (Application.java)」 -Call View to create an input form.
public static Result setitem() {
Form<Message> f = new Form(Message.class);
return.ok(item.render("Enter your ID number.",f));
}
「View (item.scala.html)」 -Render the screen structure of the input form ・ When you press the send button, the data will be sent to edit. ・ Originally, name and message do not need to be entered this time, but since they are required input items due to annotation, they can be hidden.
@(msg:String,form1:Form[models.Message])//Give form1 a form function
@main("Sample Page"){
<h1>input ID Number</h1>
<p>@msg</p>
@helper.form(action = routes.Application.edit){
@(helper.inputText(
field = form1("id")//Form is output
))
<input type="hidden" value = "dummy" name="name"/>
<input type="hidden" value = "dummy" name="message"/>
<input type="submit">
}
}
「routes」
POST /edit controllers.Application.edit()
「Conotrller (Application.java)」 -Call view and render the input state of the entity hit by ID search.
public static Result edit() {
Form<Message> f = new Form(Message.class).bindFromRequest();
if (!f.hasErrors()) {
Message obj = f.get();//obj contains the entered value
Long id = obj.id;//Extract id from obj
obj = Message.find.byId(id);//Search for an entity by id and enter the search result in obj
if (obj != null) {
f = new Form(Message.class).fill(obj);//Include the entered entity in the Form
return ok(edit.render("ID=" + id + "Edit the post.", f));
} else {
return ok(item.render("ERROR:ID post not found.", f));
}
} else {
return ok(item.render("ERROR:There is a problem with the input.", f));
}
}
「View (edit.scala.html)」 -Render the screen structure of the input form with the entity entered -When the send button is pressed, data is sent to update.
@(msg:String,form1:Form[models.Message])
@main("Sample page"){
<h1>Update Data</h1>
<p>@msg</p>
@helper.form(action=routes.Application.update){
<input type="hidden" value="@form1("id").value" name="id"/>//Specifying the entity to be updated
@(helper.inputText(
field = form1("name")
))
@(helper.inputText(
field = form1("mail")
))
@(helper.textarea(
field = form1("message")
))
<input type="submit">
}
}
「routes」
POST /update controllers.Application.update()
「Conotrller (Application.java)」 ・ Update the data and move to the first screen with return
public static Result update() {
Form<Message> f = new Form(Message.class).bindFromRequest();
if (!f.hasErrors()) {
Message data = f.get();
data.update();
return redirect("/");
} else {
return ok(edit.render("ERROR:Please enter again.", f));
}
}
「routes」
GET /del controllers.Application.delete()
POST /remove controllers.Application.remove()
「Conotrller (Application.java)」
public static Result delete() {
Form<Message> f = new Form(Message.class);
return ok(delete.render("Number to delete", f));
}
}
「View (delete.scala.html)」 -Render the deletion reception screen ・ When the send button is pressed, the data is sent to remove.
@(msg:String,form1:Form[models.Message])
@main("Sample Page"){
<h1>delete data</h1>
<p>@msg</p>
@helper.form(action = routes.Application.remove){
@(helper.inputText(
field = form1("id")
))
<input type="hidden" value="dummy" name="name"/>
<input type="hidden" value = "dummy" name="message"/>
<input type="submit">
}
}
public static Result remove() {
Form<Message> f = new Form(Message.class).bindFromRequest();
if (!f.hasErrors()) {
Message obj = f.get();
Long id = obj.id;
obj = Message.find.byId(id);
if (obj != null) {
obj.delete();
return redirect("/");
} else {
return ok(delete.render("ERROR:The ID number cannot be found.", f));
}
} else {
return ok(delete.render("ERROR:There was an error in the input", f));
}
}
Recommended Posts