図解で学ぶ Spring MVC / Architecture of Spring MVC
by
otty375
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
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