Play Framework studying value passing, form helper

(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

How to receive a value from the controller

How to pass the value to the class of the controllers folder → the class of the view folder in the picture below.

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

Processing on the view folder side

-Prepare a variable that can receive arguments like this. ・ If there are multiple, prepare multiple. -Be sure to set the type to the wrapper class type.

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

How to describe controllers

Describe the value you want to pass in this way in `ʻindex.render``.

  public static Result index() {
        return ok(index.render(
        "Sample 1",
        "Sample 2",
        "Sample 3"
        )
  }

How to create a form using the form helper

How to write index.scala.html

@main("sample") {

    <h1>Hello!</h1>
    <p>@one</p>
    <p>@two</p>
    <p>@three</p>
    <p>This is a sample</p>

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

action attribute: Specify the destination of the data to be sent by pressing the submit button of the form. (Reference) https://qiita.com/mikuhonda/items/f3126380d3340f3d8a2b input element: Depending on what value is specified for the type attribute, it is possible to specify the type of form parts such as one-line text box, check box, radio button, submit button, reset button, etc. (Reference) http://www.htmq.com/html5/input.shtml

① Call the form object in helper. The action usually specifies the destination address, but here it determines the method to send. (2) Create a text box and store the value in message. ③ Create a send button

How to describe controllers

Rewrite like this

package controllers;

import play.*;
import play data.*;//PKG for using form helpers
import static plya.data.Form.*;//PKG for using form helpers
import play.mvc.*;

import views.html.*;

public class Application extends Controller {


    //Inner class for Form
    public static class SampleForm{//Inner class, class for managing the value of submitted form
      public String message;//import type="text"Variable for storing tag values

    //Action when accessing the route
    public static Result index(){
      return ok(index.render("Write something.",new Form(SampleForm.class)));
      //Create a Form instance by specifying SampleForm class as an argument
    }

    //Action when accessing 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)));
      }
    }
}

processing of send method

    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 {//Tell the client that an error has occurred
      return badRequest(index.render)("ERROR",form(SampleForm.class)));
      }
    }

What is binding: Image of being assigned (Reference) http://wa3.i-3-i.info/word12448.html

(1) SampleForm is limited in type by generics. In bindFormRequest (), return the value of the form submitted from the client in the bound Form instance. After pressing the send button → go to the root file send → April send is called. ② The error check is entered first (3) By calling get of Form, you can get an instance of the class (SampleForm class) that manages form information. ④

How to describe the route

# 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()

About the difference between POST and GET → POST is used when registering information on the server. GET is used to get information from the server. (Reference) https://qiita.com/Sekky0905/items/dff3d0da059d6f5bfabf

Recommended Posts

Play Framework studying value passing, form helper
Play Framework studying test
Play Framework study
Play Framework2.5 (Java) Tips
play framework personal notes
Thymeleaf th: field meaning and Spring Framework value passing mechanism