LoginSignup
5
1

More than 5 years have passed since last update.

Play Framework勉強めも DBの更新について

Last updated at Posted at 2018-03-31

(参考)勉強してる書籍
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

エンティティの更新方法

大まかな流れ

一番シンプルなのが、以下の方法
1.IDで検索
2.入力内容の更新

コード

IDで更新対象のエンティティを検索する

「routes」

GET     /item                       controllers.Application.setitem()

「Conotrller (Application.java)」
・Viewを呼び出して、入力フォームを作成。

    public static Result setitem() {
        Form<Message> f = new Form(Message.class);
        return.ok(item.render("ID番号を入力。",f));
    }

「View (item.scala.html)」
・入力フォームの画面構造をレンダリングする
・送信ボタンを押下した際、editにデータを送付する
・今回本来ならnameとmessageは入力不要だが、アノテーションにより必須入力項目のため、hiddenで対応

@(msg:String,form1:Form[models.Message])//form1にフォーム機能を持たせる

@main("Sample Page"){

    <h1>input ID Number</h1>
    <p>@msg</p>
    @helper.form(action = routes.Application.edit){

      @(helper.inputText(
        field = form1("id")//フォームが出力される
      ))
      <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)」
・viewを呼び出して、ID検索でヒットしたエンティティが、入力された状態をレンダリングする。

    public static Result edit() {
        Form<Message> f = new Form(Message.class).bindFromRequest();
        if (!f.hasErrors()) {
            Message obj = f.get();//objには入力された値が入る
            Long id = obj.id;//objからidを取り出す
            obj = Message.find.byId(id);//idでエンティティを検索し、objに検索結果が入る
            if (obj != null) {
                f = new Form(Message.class).fill(obj);//入力された状態のエンティティをFormに組み込み
                return ok(edit.render("ID=" + id + "の投稿を編集。", f));
            } else {
                return ok(item.render("ERROR:IDの投稿が見つかりません。", f));
            }
        } else {
            return ok(item.render("ERROR:入力に問題があります。", f));
        }
    }

「View (edit.scala.html)」
・エンティティが入力された状態の入力フォームの画面構造をレンダリングする
・送信ボタンを押下した際、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"/>//更新対象のエンティティを指定している

      @(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)」
・データを更新して、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:再度入力ください。", 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("削除する番号", f));
    }
}

「View (delete.scala.html)」
・削除受付画面をレンダリングする
・送信ボタンを押下した際、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:そのID番号は見つかりません。", f));
            }
        } else {
            return ok(delete.render("ERROR:入力にエラーが起こりました", f));
        }
    }
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1