図解で学ぶ Spring MVC / Architecture of Spring MVC

58c7dad4dc33a4aeec9f4e63e9355528?s=47 otty375
August 28, 2019

図解で学ぶ Spring MVC / Architecture of Spring MVC

58c7dad4dc33a4aeec9f4e63e9355528?s=128

otty375

August 28, 2019
Tweet

Transcript

  1. ਤղͰֶͿ Spring MVC 2019/08/28 JSUGษڧձ 2019ͦͷ8 Spring for Beginner #jsug

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

  3. ࠓ೔ͷΰʔϧ • Spring MVC ͷΞʔΩςΫνϟ͕ΠϝʔδͰ͖Δ • Spring MVC ͷجຊతͳ࣮૷ํ๏͕ཧղͰ͖Δ 

     ʘࠓޙSpring MVC Λֶश͢Δ্Ͱͷ౔୆ʹͳΕ͹ʗ
  4. ΞδΣϯμ 1. Spring MVC ͱ͸ 2. Spring MVC ͷΞʔΩςΫνϟ 3.

    Spring MVC ͷجຊతͳ࣮૷ํ๏ 4. Spring MVC ͷͦͷଞͷػೳ 5. ·ͱΊ  
  5. Spring MVC ͱ͸  

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

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

  8. Spring MVC ͷ ΞʔΩςΫνϟ  

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

    
  10. Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ

    ᶇ ᶈ ᶉ ᶊ ᶋ ։ൃऀ͕࣮૷͢Δ෦෼ Spring MVC ಺෦ ᶌ ᶎ ᶍ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD
  11. ॲཧͷྲྀΕ ͦͷ̍ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

    ᶉ ᶊ ᶋ ᶌ ᶎ ᶍ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ
  12. DispatcherServlet • શͯͷϦΫΤετͷΤϯτϦʔϙΠϯτͱͳΔ αʔϒϨοτΫϥε • ༷ʑͳ໾ׂͷΠϯλʔϑΣʔεΛݺͼग़ͯ͠ ϑϨʔϜϫʔΫશମΛ੍ޚ͍ͯ͠Δ  

  13. ॲཧͷྲྀΕ ͦͷ̍ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

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

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ
  15. HandlerMapping • ࢖༻͢ΔHandlerΛಛఆ͢Δ໾ׂͷ ΠϯλʔϑΣʔε • ύε΍HTTPϝιουͳͲΛݩʹɹɹ ͲͷHandlerΛݺͼग़͔͢ಛఆ͢Δ  

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

  17. ॲཧͷྲྀΕ ͦͷ̎ %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Λ ڭ͍͑ͯͩ͘͞ʯ
  18. ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ
  19. HandlerAdapter • HandlerΛݺͼग़͢໾ׂͷΠϯλʔϑΣʔε • ݺͼग़͠લޙʹదٓඞཁͳॲཧΛߦ͏ • ϦΫΤετϘσΟΛJavaΦϒδΣΫτʹม׵ • Bean Validation

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

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

  22. ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

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

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

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ
  25. ViewResolver • Handler͔Βฦ٫͞ΕͨView໊ΛݩʹɺView ͷ࣮૷ΫϥεΛಛఆ͠Viewͷ׬શύεΛฦ͢ ໾ׂ  

  26. ॲཧͷྲྀΕ ͦͷ̐ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

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

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶎ ᶍ
  28. View • Ϩεϙϯεσʔλͷੜ੒Λߦ͏໾ׂ • ModelͷதͷσʔλΛࢀরͯ͠ςϯϓϨʔτ ʹຒΊࠐΉ  

  29. ॲཧͷྲྀΕ ͦͷ̑ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ 7JFX ʮ.PEFMͷσʔλͱUFNQMBUFTHSFFUIUNM Λ࢖ͬͯϨεϙϯεΛੜ੒͠·͢ʯ ᶎ ᶍ .PEFM message = "hello" <p th:text=“${message}"></p> ᶌ %JTQBUDIFS4FSWMFU ʮϨεϙϯεͷੜ੒Λ ͓ئ͍͠·͢ʯ
  30. ॲཧͷྲྀΕ ͦͷ̑ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ %JTQBUDIFS4FSWMFU ʮϨεϙϯεͱͯ͠HSFFUIUNMΛฦ͠·͢ʯ ᶎ ᶍ 7JFX ʮϨεϙϯε͕׬੒͠·ͨ͠ʯ ᶍ <p>hello</p>
  31. Spring MVC ͷ جຊతͳ࣮૷ํ๏  

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

     
  33. • Mavenͷ৔߹ɿpom.xmlͷdependenciesʹ௥Ճ 1. Spring MVC ͷґଘ௥Ճ   <dependencies> <dependency>

    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!— ͦͷଞඞཁͳґଘΛهࡌɻࠓճ͸ςϯϓϨʔτΤϯδϯʹThymeleafΛ࢖༻ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
  34. 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
  35. 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Λ෇༩͢Δ
  36. 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ͳͲ
  37. 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ΛҾ਺Ͱड͚औΔ
  38. 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 ඞཁͳϏδωεϩδοΫΛ࣮૷ ʢ͜͜Ͱ͸ݱࡏ࣌ࠁʹԠͯ͡ҟͳΔϝοηʔδΛ ฦ͢Α͏ͳϝιουʣ
  39. 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ͱ͍͏໊લͰ஋Ληο τʣ
  40. 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໊Λฦ͢
  41. 3. View࣮૷   <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta

    charset="UTF-8" /> <title>Greet</title> </head> <body> <p th:text="${message}"></p> </body> </html> greet.html ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ
  42. 3. View࣮૷   <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta

    charset="UTF-8" /> <title>Greet</title> </head> <body> <p th:text="${message}"></p> </body> </html> greet.html ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ )BOEMFSͰฦ٫͞ΕΔ7JFX໊ʹରԠ͢ΔϑΝΠϧΛ ࡞੒
  43. 3. View࣮૷   <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta

    charset="UTF-8" /> <title>Greet</title> </head> <body> <p th:text="${message}"></p> </body> </html> greet.html .PEFMʹηοτ͞ΕΔ஋ΛຒΊࠐΉهࡌ ʢ͜ͷ৔߹͸ల։͞ΕΔͱ QIFMMPQͷΑ͏ʹͳΔʣ ʢςϯϓϨʔτΤϯδϯɿThymeleafʣ
  44. ิ଍ɿඞཁͳConfiguration • ࠓճ͸Spring BootͷAutoConfigurationػೳʹ ΑΓɺඞཁͳConfigurationͳͲ͸Spring Boot ͕༻ҙ͍ͯ͠ΔσϑΥϧτͷ΋ͷΛ࢖༻͢Δ͜ ͱͱ͠·͢  

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

    •  
  46. ิ଍ɿRestController • REST APIͷ৔߹͸@RestControllerΛ࢖༻ • ϝιουͷ໭Γ஋͸View໊Ͱ͸ͳ͘ɹɹɹ JavaΦϒδΣΫτͳͲͷϨεϙϯεϘσΟ • JavaΦϒδΣΫτ <->

    JSON ͷม׵͸ HandlerAdapterͷ෦෼ͰߦΘΕΔ  
  47. Spring MVC ͷ ͦͷଞͷػೳ  

  48. Spring MVC ͷͦͷଞͷػೳ • ҎԼͷΑ͏ͳجຊతͳػೳ΋͋Γ·͢ • όϦσʔγϣϯ • ྫ֎ϋϯυϥ •

    RESTΫϥΠΞϯτʢRestTemplateʣ • ςετʢMockMvcʣ ͳͲ  
  49. Spring MVC ͷͦͷଞͷػೳ • ҎԼͷΑ͏ͳԠ༻తͳػೳ΋͋Γ·͢ • ηογϣϯͰͷσʔλ؅ཧ • ඇಉظॲཧ •

    ࠃࡍԽɹͳͲ  
  50. ·ͱΊ  

  51. Spring MVC ·ͱΊ • WebΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹར༻͢Δ ϑϨʔϜϫʔΫ • DispatcherServlet͕શϦΫΤετͷΤϯτϦʔ ϙΠϯτͰϑϨʔϜϫʔΫશମΛ੍ޚ͍ͯ͠Δ •

    ϦΫΤετϚοϐϯάͳͲΛΞϊςʔγϣϯΛ ར༻ͯ͠ઃఆͰ͖Δ  
  52. Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ

    ᶇ ᶈ ᶉ ᶊ ᶋ ։ൃऀ͕࣮૷͢Δ෦෼ Spring MVC಺෦ ᶌ ᶎ ᶍ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD
  53. ࢀߟࢿྉ  

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

  55. ࢀߟࢿྉʢWebʣ • Spring ެࣜαΠτ • https://spring.io/ • ॳΊͯͰ΋30෼Ͱ෼͔ΔSpring 5 &

    Spring Boot 2Φʔ όʔϏϡʔ • https://www.slideshare.net/ masatoshitada7/30spring-5-spring- boot-2-103523666  
  56. EOP