[JAVA] Play Framework study

(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

What is MVC architecture?

An application that is built as a combination of "Model", "View", and "Controller". "Model" and "View" are controlled by "Controller". Model ・ Manage data -Undertake the processing of data used in the application

View ・ Undertake the part related to screen display

Controller ・ Those that control the entire application

About Controller class

(Example) Screen creation process


package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
/*
(1) Get an instance of HTML class with the render method of index instance.
(2) Return an instance of Result type with the ok method.
*/
    }
}

-Render method: render (argument) Renders a web page template based on arguments. The return value is an HTML instance.

What is an action

An action is like a method for creating a screen. Most of the requests received by the Play application are processed by Action. The action returns a value of type play.mvc.Result, which represents the HTTP response sent to the client.

About the view class

A class that creates the appearance of a page. This time, it is roughly divided into the following two. ・ ** main.scala.html ** → Layout of the entire page ・ ** Index.scala.html ** → Display content with Action` of ʻindex

The image is rented with the contents of ʻindex.scala.html embedded in main.scala.html when Action of ʻindex is called. By dividing the layout of the entire page into main.scala.html and the partial design into ʻindex.scala.html`, only the content can be output in the common layout.

main.scala.html


@(title: String)(content: Html)//Variable declaration

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">//loading css
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png ")">//Loading favicon
        <script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script>//Loading javascript
    </head>
    <body>
        @content//The content read by the head is output by the body.
    </body>
</html>

-By adding @ to the acronym, you can describe the scala method. -In the Play application, each file is published with its own set address. Read them with @ routes.Assets.at. (reference) ・ What is link doug: https://saruwakakun.com/html-css/basic/link-rel ・ What is a favicon: http://www.htmq.com/tutorial/28_9.shtml -If you create a css file in the view folder, it will be automatically generated as a scala object at the time of template.

・ ** Index.scala.html **


@(message: String)

@main("Welcome to Play") {//The argument is main.scala.Enter the html title

    @play20.welcome(message, style = "Java")//main.scala.Enter the html body

}

・ @Main is for calling main.scala.html -"Welcome to Play" in main ("Welcome to Play") is in the title of main.scala.html. ・ The inside of {} is (content: Html). ・ @ Play20.welcome is for generating a page called welcome page.

About the route class

A file that describes the processing order of operations.


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

# Home page
GET     /                           controllers.Application.index()
//If the root is called with GET, the index action is called
# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
//If GET calls any file in the publi folder, Assets.at method is called

・ Statements starting with # are comment statements

-The method is called in [Get address calling method].

-If you add a new Action or method, be sure to describe it here.

Process flow

  1. get is called for the web server
  2. From the conf / routes file, you can see that you should call index () of Aplication.java for "/" of Get. 3. index () of Aplication.java is called.
  3. index.render is returned by index () of Aplication.java
  4. That is, views / index.scala.html is returned
  5. index.scala.html always refers to main.scala.html

Recommended Posts

Play Framework study
Play Framework Study Memo Database ②Read
Play Framework Study Momo DB Update
Play Framework2.5 (Java) Tips
Play Framework studying test
play framework personal notes
Introduced Dozer to Play Framework
Validation function in Play Framework
spring framework Simple study memo (2): AOP
Play Framework 2.6 (Java) development environment creation
Double submit measures with Play Framework
Play framework scheduled jobs and crontab
Authentication function in Play Framework [Access restrictions]
java framework
Stream play
Play Framework 2.6 (Java) environment construction in Eclipse
Spring Framework study notes [Part 1] DI container
Authentication function with Play Framework [Registration and authentication]
Post to Slack from Play Framework 2.8 (Java)
Play Framework studying value passing, form helper
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
Handle JSON in cross domain with Play Framework