Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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

otty375
August 28, 2019

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

otty375

August 28, 2019
Tweet

More Decks by otty375

Other Decks in Technology

Transcript

  1. ΞδΣϯμ 1. Spring MVC ͱ͸ 2. Spring MVC ͷΞʔΩςΫνϟ 3.

    Spring MVC ͷجຊతͳ࣮૷ํ๏ 4. Spring MVC ͷͦͷଞͷػೳ 5. ·ͱΊ  
  2. Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ

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

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

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

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ ᶍ ᶎ
  6. ॲཧͷྲྀΕ ͦͷ̎ %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Λ ڭ͍͑ͯͩ͘͞ʯ
  7. ॲཧͷྲྀΕ ͦͷ̏ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶄ ᶅ ᶆ ᶇ ᶈ

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

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

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

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

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

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

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

    ᶉ ᶊ ᶋ ᶌ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD ᶃ %JTQBUDIFS4FSWMFU ʮϨεϙϯεͱͯ͠HSFFUIUNMΛฦ͠·͢ʯ ᶎ ᶍ 7JFX ʮϨεϙϯε͕׬੒͠·ͨ͠ʯ ᶍ <p>hello</p>
  15. • 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>
  16. 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
  17. 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Λ෇༩͢Δ
  18. 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ͳͲ
  19. 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ΛҾ਺Ͱड͚औΔ
  20. 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 ඞཁͳϏδωεϩδοΫΛ࣮૷ ʢ͜͜Ͱ͸ݱࡏ࣌ࠁʹԠͯ͡ҟͳΔϝοηʔδΛ ฦ͢Α͏ͳϝιουʣ
  21. 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ͱ͍͏໊લͰ஋Ληο τʣ
  22. 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໊Λฦ͢
  23. 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ʣ
  24. 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໊ʹରԠ͢ΔϑΝΠϧΛ ࡞੒
  25. 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ʣ
  26. Spring MVC ͷΞʔΩςΫνϟ %JTQBUDIFS 4FSWMFU )BOEMFS.BQQJOH ᶃ ᶄ ᶅ ᶆ

    ᶇ ᶈ ᶉ ᶊ ᶋ ։ൃऀ͕࣮૷͢Δ෦෼ Spring MVC಺෦ ᶌ ᶎ ᶍ   )BOEMFS"EBQUFS 7JFX3FTPMWFS 7JFX )BOEMFS IUNMFUD
  27. ࢀߟࢿྉʢWebʣ • Spring ެࣜαΠτ • https://spring.io/ • ॳΊͯͰ΋30෼Ͱ෼͔ΔSpring 5 &

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