LoginSignup
2
0

More than 5 years have passed since last update.

Play Framework勉強めも値の受け渡し、フォームヘルパー

Last updated at Posted at 2018-03-24

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

コントローラから値を受け取る方法

下の写真でいうところの、controllersフォルダのクラス→viewフォルダのクラスに値を渡す方法。

スクリーンショット 2018-03-18 19.33.13.png

viewフォルダ側の処理

・こんな風に引数を受け取れる変数を用意する。
・複数ある場合は、複数用意する。
・型は必ずラッパークラス型に設定する。

@(one: String ,two :String, three:String)

controllersの記載方法

こんな風に受け渡したい値をindex.renderの中に記述する。

  public static Result index() {
        return ok(index.render(
        "サンプル1",
        "サンプル2",
        "サンプル3"
        )
  }

フォームヘルパーを使って、フォームを作成する方法

index.scala.htmlの記載方法

@main("サンプル") {

    <h1>Hello!</h1>
    <p>@one</p>
    <p>@two</p>
    <p>@three</p>
    <p>これはサンプル</p>

    @helper.form(action=routes.Application.send){<!--①-->
    @(helper.inputText(<!--②-->
      field = form1("message")
    ))
    <input type="submit"><!--③-->
    }
}

action属性:フォームの送信ボタンを押して送信されるデータの送信先を指定する。
(参考)https://qiita.com/mikuhonda/items/f3126380d3340f3d8a2b
input要素:type属性にどのような値を指定するかによって、 一行テキストボックス・チェックボックス・ラジオボタン・送信ボタン・リセットボタン等、 フォーム部品の種類を指定し分けることができます
(参考)http://www.htmq.com/html5/input.shtml

①helperにあるformオブジェクトを呼び出し。actionは通常、送信先のアドレスを指定するがここでは、送信するメソッドを決める。
②テキストボックスを作成し、値をmessageに格納する。
③送信ボタンを作成する

controllersの記載方法

こんな風に書き換える

package controllers;

import play.*;
import play data.*;//フォームヘルパーを利用するためのPKG
import static plya.data.Form.*;//フォームヘルパーを利用するためのPKG
import play.mvc.*;

import views.html.*;

public class Application extends Controller {


    //Form用の内部クラス
    public static class SampleForm{//内部クラス、送信されたフォームの値を管理するためのクラス
      public String message;//import type="text"タグの値を保管するための変数

    //ルートにアクセスした際のAction
    public static Result index(){
      return ok(index.render("何か書いて。",new Form(SampleForm.class)));
      //引数にSampleFormクラスを指定して、Formインスタンスを生成する
    }

    //sendにアクセスした際のAction
    public static Result send(){
      Form<SampleForm> f = form(SampleForm.class).bindFormRequest();
      if(!f.hasErrors()){
      }
      SampleForm date = f.get();
      String msg = "you typed:" + date.message;
      return ok(index.render(msg,f));
      }else {
      return badRequest(index.render)("ERROR",form(SampleForm.class)));
      }
    }
}

sendメソッドの処理

    public static Result send(){
      Form<SampleForm> f = form(SampleForm.class).bindFormRequest();//①
      if(!f.hasErrors()){//②
      SampleForm date = f.get();//③
      String msg = "you typed:" + date.message;//④
      return ok(index.render(msg,f));
      }else {//エラーが発生した事をクライアントに伝える
      return badRequest(index.render)("ERROR",form(SampleForm.class)));
      }
    }

バインドとは:割り当てられるとかのイメージ
(参考)http://wa3.i-3-i.info/word12448.html

①SampleFormはジェネリクスで型を限定している。bindFormRequest()で、クライアントから送信されたフォームの値を、バインドしたFormインスタンスで返す。
送信ボタンを押したら→ルートファイルのsendに行って→Apricationのsendが呼び出される。
②最初にエラーチェックが入る
③Formのgetを呼び出すと、フォーム情報を管理するクラス(SampleFormクラス)のインスタンスが取得出来る。

ルートの記載方法

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

POST    /send                       controllers.Application.send()

POSTとGETの違いについて
→POSTはサーバに情報を登録する際に使用する。GETはサーバから情報を取得して来る際に使用する。
(参考)https://qiita.com/Sekky0905/items/dff3d0da059d6f5bfabf

2
0
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
2
0