Slide 1

Slide 1 text

ਤղͰֶͿ Spring MVC 2019/08/28 JSUGษڧձ 2019ͦͷ8 Spring for Beginner #jsug ͍͚ @otty_375

Slide 2

Slide 2 text

ࣗݾ঺հ • ͍͚ʢ@otty_375ʣ • ITاۀͰWebγεςϜ։ൃӡ༻ • Springྺ͸2೥΄Ͳ

Slide 3

Slide 3 text

ࠓ೔ͷΰʔϧ • Spring MVC ͷΞʔΩςΫνϟ͕ΠϝʔδͰ͖Δ • Spring MVC ͷجຊతͳ࣮૷ํ๏͕ཧղͰ͖Δ ʘࠓޙSpring MVC Λֶश͢Δ্Ͱͷ౔୆ʹͳΕ͹ʗ

Slide 4

Slide 4 text

ΞδΣϯμ 1. Spring MVC ͱ͸ 2. Spring MVC ͷΞʔΩςΫνϟ 3. Spring MVC ͷجຊతͳ࣮૷ํ๏ 4. Spring MVC ͷͦͷଞͷػೳ 5. ·ͱΊ

Slide 5

Slide 5 text

Spring MVC ͱ͸

Slide 6

Slide 6 text

Spring MVC ͱ͸ • WebΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹར༻͢Δ ϑϨʔϜϫʔΫ • ϑϩϯτίϯτϩʔϥύλʔϯΛ࠾༻ • ϦΫΤετϚοϐϯάͳͲΛΞϊςʔγϣϯΛ ར༻ͯ͠ઃఆͰ͖Δ

Slide 7

Slide 7 text

ϑϩϯτίϯτϩʔϥύλʔϯ • શͯͷϦΫΤετͷΤϯτϦʔϙΠϯτͱͯ͠ ϑϩϯτίϯτϩʔϥΛ࢖༻͢Δ • ద੾ͳ໾ׂͷΦϒδΣΫτʹॲཧΛҕৡ͢Δ • ϝϦοτɿϦΫΤετʹؔ͢Δॲཧͷڞ௨Խ

Slide 8

Slide 8 text

Spring MVC ͷ ΞʔΩςΫνϟ

Slide 9

Slide 9 text

ॲཧͷྲྀΕ ҎԼͷαϯϓϧΞϓϦΛྫʹઆ໌͠·͢ • ϦΫΤετɿ/greet [GET] • Ϩεϙϯεɿgreet.html • ࣌ؒଳʹΑͬͯҟͳΔݴ༿Λฦ͢

Slide 10

Slide 10 text

Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ։ൃऀ͕࣮૷͢Δ෦෼ Spring MVC ಺෦ ᶌ ᶎ ᶍ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD

Slide 11

Slide 11 text

ॲཧͷྲྀΕ ͦͷ̍ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ ᶎ ᶍ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ

Slide 12

Slide 12 text

DispatcherServlet • શͯͷϦΫΤετͷΤϯτϦʔϙΠϯτͱͳΔ αʔϒϨοτΫϥε • ༷ʑͳ໾ׂͷΠϯλʔϑΣʔεΛݺͼग़ͯ͠ ϑϨʔϜϫʔΫશମΛ੍ޚ͍ͯ͠Δ

Slide 13

Slide 13 text

ॲཧͷྲྀΕ ͦͷ̍ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ ᶎ ᶍ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ΫϥΠΞϯτ ʮHSFFUΛऔಘ͍ͨ͠Ͱ͢ʯ %JTQBUDIFS4FSWMFU ʮྃղͰ͢ʯ

Slide 14

Slide 14 text

ॲཧͷྲྀΕ ͦͷ̎ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ

Slide 15

Slide 15 text

HandlerMapping • ࢖༻͢ΔHandlerΛಛఆ͢Δ໾ׂͷ ΠϯλʔϑΣʔε • ύε΍HTTPϝιουͳͲΛݩʹɹɹ ͲͷHandlerΛݺͼग़͔͢ಛఆ͢Δ

Slide 16

Slide 16 text

Handler • ϦΫΤετʹର͢Δ۩ମతͳॲཧΛߦ͏໾ׂ • ։ൃऀ͕࣮૷͢Δ෦෼ • ϦΫΤετύε΍HTTPϝιουͳͲͷɹɹɹ ϚοϐϯάઃఆΛߦ͏

Slide 17

Slide 17 text

ॲཧͷྲྀΕ ͦͷ̎ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ )BOEMFS.BQQJOH ʮ(SFFU$POUSPMMFSͷHSFFUϝιουͰ͢ʯ Ͳͷ)BOEMFS͔ʁ ύε )551ϝιου ύϥϝʔλ )FMMP$POUSPMMFS ͷIFMMPϝιου IFMMP (&5 ͳ͠ (SFFU$POUSPMMFS ͷHSFFUϝιου HSFFU (&5 ͳ͠ ʜ ʜ ʜ ʜ %JTQBUDIFS4FSWMFU ʮHSFFUͷ(&5ϝιουͷ)BOEMFSΛ ڭ͍͑ͯͩ͘͞ʯ

Slide 18

Slide 18 text

ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ

Slide 19

Slide 19 text

HandlerAdapter • HandlerΛݺͼग़͢໾ׂͷΠϯλʔϑΣʔε • ݺͼग़͠લޙʹదٓඞཁͳॲཧΛߦ͏ • ϦΫΤετϘσΟΛJavaΦϒδΣΫτʹม׵ • Bean Validation ͳͲ

Slide 20

Slide 20 text

Handler • ϦΫΤετʹର͢Δ۩ମతͳॲཧΛߦ͏໾ׂ • ViewΛ࢖༻͢Δ৔߹ɺ໭Γ஋͸Viewͷ໊લΛ ฦ͠ը໘ͷදࣔʹඞཁͳσʔλ͸Modelͷதʹ ٧Ίͯฦ͢

Slide 21

Slide 21 text

Model • Handler͔ΒViewςϯϓϨʔτʹ஋Λड͚౉͢ ͨΊʹར༻͞ΕΔΠϯλʔϑΣʔε • Modelͷதͷ৘ใ͸View͕ݟΕΔҐஔʹஔ͔Ε Viewͷ૊Έཱͯʹ࢖ΘΕΔ

Slide 22

Slide 22 text

ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ )BOEMFS"EBQUFS ʮඞཁͳલॲཧ͸͓͍ͯͨ͠ͷͰ ॲཧΑΖ͓͘͠ئ͍͠·͢ʯ ᶍ ᶎ ᶆ %JTQBUDIFS4FSWMFU ʮ(SFFU$POUSPMMFSͷHSFFUϝιουͷ ݺͼग़͠Λ͓ئ͍͠·͢ʯ

Slide 23

Slide 23 text

ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ )BOEMFS ʮॲཧऴΘΓ·ͨ͠ 7JFXͷ໊લ͸HSFFUͰ͢ ඞཁͳσʔλ͸.PEFMʹ٧Ί͓͖ͯ·ͨ͠ʯ ᶍ ᶎ .PEFM message = "hello" ᶉ )BOEMFS"EBQUFS ʮඞཁͳޙॲཧ͸͓͖ͯ͠·ͨ͠ 7JFXͷ໊લ͸HSFFUͰ͢ʯ .PEFM

Slide 24

Slide 24 text

ॲཧͷྲྀΕ ͦͷ̐ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ

Slide 25

Slide 25 text

ViewResolver • Handler͔Βฦ٫͞ΕͨView໊ΛݩʹɺView ͷ࣮૷ΫϥεΛಛఆ͠Viewͷ׬શύεΛฦ͢ ໾ׂ

Slide 26

Slide 26 text

ॲཧͷྲྀΕ ͦͷ̐ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ 7JFX3FTPMWFS ʮUFNQMBUFTHSFFUIUNMͰ͢ʯ ᶎ ᶊ %JTQBUDIFS4FSWMFU ʮ7JFX໊͕HSFFUͳͷͰ͕͢ ۩ମతͳύεΛڭ͍͑ͯͩ͘͞ʯ

Slide 27

Slide 27 text

ॲཧͷྲྀΕ ͦͷ̑ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶎ ᶍ

Slide 28

Slide 28 text

View • Ϩεϙϯεσʔλͷੜ੒Λߦ͏໾ׂ • ModelͷதͷσʔλΛࢀরͯ͠ςϯϓϨʔτ ʹຒΊࠐΉ

Slide 29

Slide 29 text

ॲཧͷྲྀΕ ͦͷ̑ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ 7JFX ʮ.PEFMͷσʔλͱUFNQMBUFTHSFFUIUNM Λ࢖ͬͯϨεϙϯεΛੜ੒͠·͢ʯ ᶎ ᶍ .PEFM message = "hello"

ᶌ %JTQBUDIFS4FSWMFU ʮϨεϙϯεͷੜ੒Λ ͓ئ͍͠·͢ʯ

Slide 30

Slide 30 text

ॲཧͷྲྀΕ ͦͷ̑ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ %JTQBUDIFS4FSWMFU ʮϨεϙϯεͱͯ͠HSFFUIUNMΛฦ͠·͢ʯ ᶎ ᶍ 7JFX ʮϨεϙϯε͕׬੒͠·ͨ͠ʯ ᶍ

hello

Slide 31

Slide 31 text

Spring MVC ͷ جຊతͳ࣮૷ํ๏

Slide 32

Slide 32 text

جຊతͳ࣮૷ํ๏ Spring BootΛ࢖༻͢Δ৔߹Λྫʹ͠·͢ 1. Spring MVCͷґଘ௥Ճ 2. Controller࣮૷ 3. View࣮૷

Slide 33

Slide 33 text

• Mavenͷ৔߹ɿpom.xmlͷdependenciesʹ௥Ճ 1. Spring MVC ͷґଘ௥Ճ org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf

Slide 34

Slide 34 text

2. Controller࣮૷ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java

Slide 35

Slide 35 text

2. Controller࣮૷ ͦͷ̍ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java #FBOొ࿥͢ΔͨΊʹ Ϋϥεʹ!$POUSPMMFSΛ෇༩͢Δ

Slide 36

Slide 36 text

2. Controller࣮૷ ͦͷ̎ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java ϦΫΤετͱͷϚοϐϯάઃఆΛ ϝιουʹ෇༩͢Δ ଞʹ͸!1PTU.BQQJOH !1VU.BQQJOHͳͲ

Slide 37

Slide 37 text

2. Controller࣮૷ ͦͷ̐ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java 7JFXͰ࢖༻͢ΔσʔλΛηοτ͢ΔͨΊͷ .PEFMΛҾ਺Ͱड͚औΔ

Slide 38

Slide 38 text

2. Controller࣮૷ ͦͷ̏ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java ඞཁͳϏδωεϩδοΫΛ࣮૷ ʢ͜͜Ͱ͸ݱࡏ࣌ࠁʹԠͯ͡ҟͳΔϝοηʔδΛ ฦ͢Α͏ͳϝιουʣ

Slide 39

Slide 39 text

2. Controller࣮૷ ͦͷ̐ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java 7JFXͰ࢖༻͢ΔσʔλΛ.PEFMʹηοτ͢Δ ʢ͜͜Ͱ͸NFTTBHFͱ͍͏໊લͰ஋Ληο τʣ

Slide 40

Slide 40 text

2. Controller࣮૷ ͦͷ̑ package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GreetController { @GetMapping("/greet") public String greet(Model model) { String message = getGreetMessage(); model.addAttribute("message", message); return "greet"; } } GreetController.java 7JFX໊Λฦ͢

Slide 41

Slide 41 text

3. View࣮૷ Greet

greet.html ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ

Slide 42

Slide 42 text

3. View࣮૷ Greet

greet.html ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ )BOEMFSͰฦ٫͞ΕΔ7JFX໊ʹରԠ͢ΔϑΝΠϧΛ ࡞੒

Slide 43

Slide 43 text

3. View࣮૷ Greet

greet.html .PEFMʹηοτ͞ΕΔ஋ΛຒΊࠐΉهࡌ ʢ͜ͷ৔߹͸ల։͞ΕΔͱ QIFMMPQͷΑ͏ʹͳΔʣ ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ

Slide 44

Slide 44 text

ิ଍ɿඞཁͳConfiguration • ࠓճ͸Spring BootͷAutoConfigurationػೳʹ ΑΓɺඞཁͳConfigurationͳͲ͸Spring Boot ͕༻ҙ͍ͯ͠ΔσϑΥϧτͷ΋ͷΛ࢖༻͢Δ͜ ͱͱ͠·͢

Slide 45

Slide 45 text

ิ଍ɿSpring Initializr • Spring Boot ϓϩδΣΫτͷ਽ܗΛੜ੒Ͱ͖Δ • ࠷ॳ͔ΒDependenciesʹ௥Ճ͢Δ͜ͱ͕Մೳ • https://start.spring.io/ •

Slide 46

Slide 46 text

ิ଍ɿRestController • REST APIͷ৔߹͸@RestControllerΛ࢖༻ • ϝιουͷ໭Γ஋͸View໊Ͱ͸ͳ͘ɹɹɹ JavaΦϒδΣΫτͳͲͷϨεϙϯεϘσΟ • JavaΦϒδΣΫτ <-> JSON ͷม׵͸ HandlerAdapterͷ෦෼ͰߦΘΕΔ

Slide 47

Slide 47 text

Spring MVC ͷ ͦͷଞͷػೳ

Slide 48

Slide 48 text

Spring MVC ͷͦͷଞͷػೳ • ҎԼͷΑ͏ͳجຊతͳػೳ΋͋Γ·͢ • όϦσʔγϣϯ • ྫ֎ϋϯυϥ • RESTΫϥΠΞϯτʢRestTemplateʣ • ςετʢMockMvcʣ ͳͲ

Slide 49

Slide 49 text

Spring MVC ͷͦͷଞͷػೳ • ҎԼͷΑ͏ͳԠ༻తͳػೳ΋͋Γ·͢ • ηογϣϯͰͷσʔλ؅ཧ • ඇಉظॲཧ • ࠃࡍԽɹͳͲ

Slide 50

Slide 50 text

·ͱΊ

Slide 51

Slide 51 text

Spring MVC ·ͱΊ • WebΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹར༻͢Δ ϑϨʔϜϫʔΫ • DispatcherServlet͕શϦΫΤετͷΤϯτϦʔ ϙΠϯτͰϑϨʔϜϫʔΫશମΛ੍ޚ͍ͯ͠Δ • ϦΫΤετϚοϐϯάͳͲΛΞϊςʔγϣϯΛ ར༻ͯ͠ઃఆͰ͖Δ

Slide 52

Slide 52 text

Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ։ൃऀ͕࣮૷͢Δ෦෼ Spring MVC಺෦ ᶌ ᶎ ᶍ )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD

Slide 53

Slide 53 text

ࢀߟࢿྉ

Slide 54

Slide 54 text

ࢀߟࢿྉʢॻ੶ʣ • Springపఈೖ໳ʢגࣜձࣾNTTσʔλ / ஶʣ

Slide 55

Slide 55 text

ࢀߟࢿྉʢWebʣ • Spring ެࣜαΠτ • https://spring.io/ • ॳΊͯͰ΋30෼Ͱ෼͔ΔSpring 5 & Spring Boot 2Φʔ όʔϏϡʔ • https://www.slideshare.net/ masatoshitada7/30spring-5-spring- boot-2-103523666

Slide 56

Slide 56 text

EOP