Slide 1

Slide 1 text

OUCC Scala × Play Framework Hands-on @spring_raining

Slide 2

Slide 2 text

એ఻ ҰॹʹWebΞϓϦ࡞Ζ͏ʂʂʂʂʂ̍ʂʂ →→→Reply to @spring_raining

Slide 3

Slide 3 text

એ఻ OUCC Advent Calendarॻ͖·ͤΜ͔ ʂʂʂʂʂʁʁʁʂʂʂʂʁʁ →http://www.adventar.org/calendars/449

Slide 4

Slide 4 text

Scalaͱ͸ • Javaͱಉ͡؀ڥͰಈ͘ • ؆ܿͳදه • ؔ਺ܕmeetsΦϒδΣΫτࢦ޲

Slide 5

Slide 5 text

Play Frameworkͱ͸ • Ruby on Rails / Django like • ͨ͘͞Μͷಋೖࣄྫ • ˒5,405

Slide 6

Slide 6 text

GETTING STARTED

Slide 7

Slide 7 text

ʮJava SEʯͰݕࡧݕࡧ ʮJDKʯΛΠϯετʔϧ

Slide 8

Slide 8 text

Θ͔Γʹ͍͘ ʮAccept License AgreementʯΛΫϦοΫ

Slide 9

Slide 9 text

typesafe.com Typesafe Activator Λμ΢ϯϩʔυ & Πϯετʔϧ

Slide 10

Slide 10 text

IDE͸͓޷ΈͰ IntelliJ IDEAΛ૝ఆͯ͠ਐΊ·͢

Slide 11

Slide 11 text

μ΢ϯϩʔυ͕ऴΘΔ·Ͱ… Scalaͷجຊߏจ

Slide 12

Slide 12 text

SYNTAX

Slide 13

Slide 13 text

ม਺ var hoge: Int = 10 var fuga = "100" println(hoge + fuga) hoge = 20 fuga = 200

Slide 14

Slide 14 text

ม਺ var hoge: Int = 10 var fuga = "100" println(hoge + fuga) hoge = 20 fuga = 200 ! var ໊લ = ஋ var ໊લ: ܕ = ஋ ܕ͸ࣗಈͰਪଌ͞ΕΔ

Slide 15

Slide 15 text

ม਺ var hoge: Int = 10 var fuga = "100" println(hoge + fuga) hoge = 20 fuga = 200 →10110

Slide 16

Slide 16 text

ม਺ var hoge: Int = 10 var fuga = "100" println(hoge + fuga) hoge = 20 fuga = 200 →ίϯύΠϧΤϥʔ fuga͸StringܕͳͷͰ Intܕ͸୅ೖͰ͖ͳ͍

Slide 17

Slide 17 text

ఆ਺ val teisuu = "konnitiwa" println(teisuu) val ໊લ = ஋ val ໊લ : ܕ = ஋ ఆ਺͸࠶୅ೖͰ͖ͳ͍

Slide 18

Slide 18 text

഑ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) Array(ॳظ஋..) new Array(഑ྻ௕) new Array[ܕ](഑ྻ௕)

Slide 19

Slide 19 text

഑ྻ val a = Array("yui", "yukari", "yuzuko") println(a(0)) println(a.length) ഑ྻͷཁૉ͸[ ]Ͱ͸ͳ͘ ( )Ͱऔಘ

Slide 20

Slide 20 text

if var foo: String = "" foo = if (1 == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if (৚݅ࣜ) ࣜ if (৚݅ࣜ) ࣜ else ࣜ if (৚݅ࣜ) { ࣜ.. } if (৚݅ࣜ) { ࣜ.. } else { ࣜ.. }

Slide 21

Slide 21 text

if var foo: String = "" foo = if (1 == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if͸ʮࣜʯͳͷͰ஋Λฦ͢ ฦ͞Εͨ஋͸ ม਺ʹ୅ೖ͞ΕΔ 

Slide 22

Slide 22 text

if var foo: String = "" foo = if (1 == 1) "yui" else "yuzuko" val bar = if (false) "yukari" println(bar) if͕஋Λฦ͞ͳ͍৔߹… Unitͱ͍͏ΫϥεΛฦ͢ (voidΈ͍ͨͳ΋ͷ)

Slide 23

Slide 23 text

for (1) val cast = Array("moffle", "macaron", "tiramy") for (i <- cast) { println(i) } for (ม਺໊ <- ίϨΫγϣϯ) { ࣜ.. }

Slide 24

Slide 24 text

for (1) val cast = Array("moffle", "macaron", "tiramy") for (i <- cast) { println(i) } ίϨΫγϣϯ͔ΒཁૉΛ 1ͭͣͭऔΓग़࣮ͯ͠ߦ (foreachΈ͍ͨͳ΍ͭ)

Slide 25

Slide 25 text

for (2) for (i <- 0 until 5) { println(i) } Scalaʹ͸͍ΘΏΔ for (ʙ ; ʙ ; ʙ) ͸࢖͑ͳ͍

Slide 26

Slide 26 text

for (2) for (i <- 0 until 5) { println(i) } ੔਺ until ੔਺ Ұ୴RangeΫϥεͰ੔਺ͷ ίϨΫγϣϯΛ࡞Γ ͔ͦ͜Β஋ΛऔΓग़͢

Slide 27

Slide 27 text

for (3) 0 until 5 ! 0 to 5 ! 0 to 5 by 2 ! 5 to 0 by -1 →Range(0, 1, 2, 3, 4) →Range(0, 1, 2, 3, 4, 5) →Range(0, 2, 4) →Range(5, 4, 3, 2, 1, 0)

Slide 28

Slide 28 text

while var i = 0 var summer = "" while (i < 10) { summer += "hiji" i += 1 } println(summer) while (৚݅ࣜ) ࣜ while (৚݅ࣜ) { ࣜ.. }

Slide 29

Slide 29 text

while var i = 0 var summer = "" while (i < 10) { summer += "hiji" i += 1 } println(summer) break΍continue͸ʁ

Slide 30

Slide 30 text

match (1) val somebody = "miyako" somebody match { case "yuno" => println(144.3) case "miyako" => println(165) case "nori" | "nazuna" => println("?") case _ => println("unknown") } ม਺ match { case ύλʔϯ => ॲཧ } match =switchͷ͍͢͝΍ͭ

Slide 31

Slide 31 text

match (1) val somebody = "miyako" somebody match { case "yuno" => println(144.3) case "miyako" => println(165) case "nori" | "nazuna" => println("?") case _ => println("unknown") } →default →nori ·ͨ͸nazuna

Slide 32

Slide 32 text

match (2) val animal = ("tanuki", "itachi", "araiguma") animal match { case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) }

Slide 33

Slide 33 text

match (2) val animal = ("tanuki", "itachi", "araiguma") animal match { case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } →λϓϧ ഑ྻʹࣅͯΔ͚Ͳ ஋ͷมߋ͸Ͱ͖ͳ͍

Slide 34

Slide 34 text

match (2) val animal = ("tanuki", "itachi", "araiguma") animal match { case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } _ ͸ϫΠϧυΧʔυ ԿͰ΋ΞϦ

Slide 35

Slide 35 text

match (2) val animal = ("tanuki", "itachi", "araiguma") animal match { case ("hakubishin", _, triple) => println("hakubishin and " + triple) case (_, "itachi", triple) => println("itachi and " + triple) case (_, _, triple) => println(triple) } Ϛονͨ͠஋͸औಘͰ͖Δ

Slide 36

Slide 36 text

match (3) val someType: Any = "Yo" someType match { case _: Int => println("seisuu") case _: String => println("mojiretsu") case _ => println("unknown") } ܕΛ൑ఆ͢Δ͜ͱ΋Մೳ

Slide 37

Slide 37 text

ͦΖͦΖμ΢ϯϩʔυ ऴΘΓͦ͏

Slide 38

Slide 38 text

activator ui ىಈ͢Δͱϒϥ΢β্ཱ͕͕ͪΔ

Slide 39

Slide 39 text

Hello Scala! ࡞੒ͨ͠ΒCode view & Open in IDE Λબ୒

Slide 40

Slide 40 text

Hello world object Hello { def main(args: Array[String]): Unit = { println("Hello, world!") } }

Slide 41

Slide 41 text

Hello world object Hello { def main(args: Array[String]): Unit = { println("Hello, world!") } } mainؔ਺

Slide 42

Slide 42 text

Hello world object Hello { def main(args: Array[String]): Unit = { println("Hello, world!") } } HelloΦϒδΣΫτ

Slide 43

Slide 43 text

㊗️

Slide 44

Slide 44 text

FUNCTION CLASS OBJECT

Slide 45

Slide 45 text

Function(1) def pow (a: Int, b: Int): Int = { if (b <= 1) a else pow(a, b - 1) * a } ! println(pow(2, 3)) def ؔ਺໊(Ҿ਺໊: ܕ, .. ): ໭ܕ = { .. } def ؔ਺໊(Ҿ਺໊: ܕ, .. ) = { .. }

Slide 46

Slide 46 text

Function(1) def pow (a: Int, b: Int): Int = { if (b <= 1) a else pow(a, b - 1) * a } ! println(pow(2, 3)) return͸লུՄ

Slide 47

Slide 47 text

Function(2) val max = (a: Int, b: Int) => { if (a > b) a else b } ! println(max(2, 3)) ؔ਺໊(Ҿ਺໊: ܕ, .. ) => { .. }: ໭ܕ ؔ਺໊(Ҿ਺໊: ܕ, .. ) => { .. }

Slide 48

Slide 48 text

Function(2) val max = (a: Int, b: Int) => { if (a > b) a else b } ! println(max(2, 3)) Scala͸ؔ਺ࣗମΛม਺ʹͨ͠Γ ฦ஋ʹͨ͠ΓͰ͖Δ =ؔ਺Ϧςϥϧ

Slide 49

Slide 49 text

Function(3) def multiply(a: Int)(b: Int) = { a * b } val twice = multiply(2) ! println(twice(3)) 1ͭͷҾ਺Ϧετ͸ෳ਺ͷ Ҿ਺ʹ෼ׂ͢Δ͜ͱ͕Ͱ͖Δ =ΧϦʔԽ

Slide 50

Slide 50 text

Function(3) def multiply(a: Int)(b: Int) = { a * b } val twice = multiply(2) ! println(twice(3)) twice͸(Int)=>Intܕͷؔ਺ ΧϦʔԽͨؔ͠਺ʹ Ҿ਺Λ෦෼తʹ༩͑Δͱ ৽͍ؔ͠਺͕࡞ΕΔ

Slide 51

Slide 51 text

Class(1) class Ship(n: String) { val name = n val level = 1 val equipments = new Array[String](4) } ! val takao = new Ship("Takao") println(takao.level) class Ϋϥε໊ = { .. } class Ϋϥε໊(Ҿ਺ఆٛ) = { .. }

Slide 52

Slide 52 text

Class(1) class Ship(n: String) { val name = n val level = 1 val equipments = new Array[String](4) } ! val takao = new Ship("Takao") println(takao.level) new Ϋϥε໊ ͰΠϯελϯεੜ੒

Slide 53

Slide 53 text

Class(2) class Ship(n: String) { val name = n } ! final class Battleship(n: String) extends Ship(n) { def explain = "[Battleship] " + name } ! val kirishima = new Battleship("Kirishima") println(kirishima explain) ܧঝɿݩ͋ΔΫϥε͔Β ৽͍͠ΫϥεΛ࡞Δ

Slide 54

Slide 54 text

Class(2) class Ship(n: String) { val name = n } ! final class Battleship(n: String) extends Ship(n) { def explain = "[Battleship] " + name } ! val kirishima = new Battleship("Kirishima") println(kirishima explain) extendͰܧঝ finalͰͦΕҎ্ܧঝͤ͞ͳ͍

Slide 55

Slide 55 text

Object(1) object RabbitHouse { var member = List("Chino", "Rize") def invite(person: String) = { member = person :: member } } ! RabbitHouse.invite("Cocoa") println(RabbitHouse.member) Scalaʹ͸staticϝιου͸ ແ͍୅ΘΓʹ objectͰγϯάϧτϯ ΦϒδΣΫτ͕࡞ΕΔ

Slide 56

Slide 56 text

Object(1) object RabbitHouse { var member = List("Chino", "Rize") def invite(person: String) = { member = person :: member } } ! RabbitHouse.invite("Cocoa") println(RabbitHouse.member) object ΦϒδΣΫτ໊ = { .. } γϯάϧτϯΦϒδΣΫτ =Πϯελϯε͕1͔ͭ͠ ࡞Εͳ͍Ϋϥε

Slide 57

Slide 57 text

Object(2) class RabbitHouse { var member = List("Chino", "Rize", "Cocoa") } ! object RabbitHouse { def apply() = new RabbitHouse } ! val rh = RabbitHouse() println(rh.member) applyϝιου͸ ݺͼग़͢ࡍ໊લΛলུͰ͖Δ

Slide 58

Slide 58 text

ଞʹ΋͍Ζ͍Ζ͋Δ͚Ͳ ͻͱ·ͣऴΘΓ

Slide 59

Slide 59 text

Hello Play!

Slide 60

Slide 60 text

Play Scala Seed μ΢ϯϩʔυ࣌ؒΉͬͪΌ͔͔Δ

Slide 61

Slide 61 text

μ΢ϯϩʔυ͕ऴΘΔ·Ͱ… WAFʹ͍ͭͯ

Slide 62

Slide 62 text

Web application framework?

Slide 63

Slide 63 text

WebΞϓϦͷجຊ Webϖʔδ ݟͤͯ HTMLͱ͔

Slide 64

Slide 64 text

ΊΜͲ͍ Security Cashing URL mapping REST Database Web template Ajax Web API

Slide 65

Slide 65 text

ͦ͜ͰWAF WebΞϓϦΛ੍࡞͢ΔࡍͷΊΜͲ͍෦෼͕ ͋Β͔͡Ί༻ҙ͞Ε͍ͯΔ

Slide 66

Slide 66 text

MVC = Model View Controller WebΞϓϦͰΑ͘࢖ΘΕΔઃܭύλʔϯ Play FrameworkͰ΋࢖ΘΕ͍ͯΔ

Slide 67

Slide 67 text

MVC Controller View Model

Slide 68

Slide 68 text

ͦΖͦΖμ΢ϯϩʔυ ऴΘΓͦ͏

Slide 69

Slide 69 text

ࠓ౓ͦ͜Hello Play μ΢ϯϩʔυͨ͠৔ॴͰactivator run localhost:9000ʹΞΫηε

Slide 70

Slide 70 text

㊗️

Slide 71

Slide 71 text

How it works?

Slide 72

Slide 72 text

PlayͷϑΝΠϧߏ଄ app/ build.sbt conf/ logs/ project/ public/ target/ test/ →ΞϓϦͷιʔείʔυ →ϏϧυεΫϦϓτ →ઃఆϑΝΠϧ →ϩάϑΝΠϧ →sbtઃఆϑΝΠϧ →Ξηοτ(ը૾ϑΝΠϧͱ͔) →ࣗಈͰੜ੒͞ΕΔϑΝΠϧ →ςετͷιʔείʔυ

Slide 73

Slide 73 text

sbt? ScalaͷϑΝΠϧΛϏϧυ͢Δπʔϧ ϥΠϒϥϦ؅ཧ΋ͯ͘͠ΕΔΒ͍͠ (Α͘஌Βͳ͍)

Slide 74

Slide 74 text

/conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) ΞΫηε͖ͯͨ͠৔ॴʹΑͬͯ ͲͷϖʔδΛݟͤΔ͔ΛܾΊΔ

Slide 75

Slide 75 text

/conf/routes GET / controllers.Application.index ! GET /assets/*file controllers.Assets.at(path="/public", file) / ʹΞΫηε͖ͯͨ͠Β controllers.Application.indexΛ࣮ߦ

Slide 76

Slide 76 text

/app/controller/Application.scala object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } } ίϯτϩʔϥʔͷ಺༰͸ ControllerΛܧঝͨ͠ΦϒδΣΫτʹ ॻ͔Ε͍ͯΔ

Slide 77

Slide 77 text

/app/controller/Application.scala object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } } → def index = Action.apply(Ok(views.html.index("..."))) index͕࣮ߦ͞ΕͨΒ Action.apply()Λฦ͢

Slide 78

Slide 78 text

/app/controller/Application.scala object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } } ϏϡʔͷindexϑΝΠϧΛ OKͷεςʔλεͰฦ͢

Slide 79

Slide 79 text

/app/views/index.scala.html @(message: String) ! @main("Welcome to Play") { ! @play20.welcome(message) ! } .htmlϑΝΠϧ͚ͩͲ ී௨ͷhtmlϑΝΠϧͰ͸ͳ͍

Slide 80

Slide 80 text

Template Engine σʔλΛݩʹ.scala.htmlςϯϓϨʔτ͔Β ग़ྗ͢ΔWebϖʔδΛੜ੒͢Δ

Slide 81

Slide 81 text

Template engine(1) @title @content ઌ಄ʹ@Λ͚ͭΔͱ ͦͷ෦෼͸Scalaͷίʔυ

Slide 82

Slide 82 text

Template engine(1) @title @content @ม਺ Ͱ ม਺ͷத਎͕ग़ྗ

Slide 83

Slide 83 text

Template engine(2)
    @for(u <- users) {
  • @u.name : @u.age
  • @if(u.age >= 20) {
    adult
    } }
@for() { .. } ͱ͔ @if() { .. } ͱ͔

Slide 84

Slide 84 text

Template engine(3) @(title: String)(content: Html) @title @content ςϯϓϨʔτ͸ؔ਺ͷΑ͏ʹ Ҿ਺ΛऔΔ͜ͱ͕Ͱ͖Δ

Slide 85

Slide 85 text

Template engine(3) @(title: String)(content: Html) @title @content ઌ಄ߦʹScalaͷؔ਺ͬΆ͘ Ҿ਺ͷఆٛΛ͢Δ

Slide 86

Slide 86 text

Template engine(4) @(message: String) ! @main("Welcome to Play") { ! @play20.welcome(message) ! } Ҿ਺Λఆٛ͢Δͱ ผϑΝΠϧ͔ΒςϯϓϨʔτͱ ಉ໊͡લͰؔ਺͕ݺ΂Δ

Slide 87

Slide 87 text

Template engine(4) @(message: String) ! @main("Welcome to Play") { ! @play20.welcome(message) ! } Ҿ਺ͱͯ͠ "Welcome to Play”ͱ play20.welcome(message) Λmainʹ༩͍͑ͯΔ

Slide 88

Slide 88 text

·ͱΊΔͱ Application.scala index.scala.html routes

Slide 89

Slide 89 text

͓ർΕ༷Ͱͨ͠ ൃද͸ऴΘΓͰ͢ ֤ࣗͰϑΝΠϧͷத਎Λม͑ͯ ͓΋͠ΖWebΞϓϦΛ࡞ͬͯΈ͍ͯͩ͘͞