Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実録Spring MVC

haljik
March 26, 2017

実録Spring MVC

haljik

March 26, 2017
Tweet

More Decks by haljik

Other Decks in Programming

Transcript

  1. ը໘͔ΒͷೖྗΛड͚औΔओཁͳํ๏ !3FRVFTU1BSBN @Controller
 @RequestMapping("/employees")
 public class EmployeeListController {
 
 //

    ex. http://localhost:8080/employees?name=haljik
 @GetMapping
 public String showList(@RequestParam("name") Name name,
 Model model) {
 model.addAttribute("employees", service.listOf(name));
 return "employees";
 }
 
 }
  2. ը໘͔ΒͷೖྗΛड͚औΔओཁͳํ๏ !1BUI7BSJBCMF @Controller
 @RequestMapping("/employees/{number}")
 public class EmployeeController {
 
 //

    ex. http://localhost:8080/employees/0001
 @GetMapping
 public String showDetail( @PathVariable("number") EmployeeNumber number, Model model ) {
 model.addAttribute("employee", service.employeeOf(number));
 return "employee-detail";
 }
 }
  3. ը໘͔ΒͷೖྗΛड͚औΔओཁͳํ๏ !.PEFM"UUSJCVUF @Controller
 @RequestMapping("/employees/registration")
 public class EmployeeRegistrationController {
 
 @ModelAttribute("employee")


    Employee employee() {
 return service.prototype();
 }
 
 @GetMapping
 public String show() {
 return "employee-registration";
 }
 
 @PostMapping
 public String register( @Valid @ModelAttribute("employee") Employee employee,
 BindingResult bindingResult ) {
 if (bindingResult.hasErrors()) return show();
 
 Employee registered = service.register(employee);
 
 return String.format("redirect:/employees/%s", registered.number());
 }
 } ᶃ ᶄ ᶆ ᶅ (&5ᶃᶄ 1045ᶅᶆ
 ᶅͱᶆͷؒͰ1PTUσʔλ͕ &NQMPZFFʹόΠϯυ͞ΕΔ !.PEFM"UUSJCVUF෇͖ͷϝιου͕ฦ͢ ΦϒδΣΫτ͕.PEFMͷଐੑ஋ͱͯ͠ηο τ͞Εͯɺ)5.-ςϯϓϨʔτ͔Βࢀর ՄೳʹͳΔ 5*14ϦΫΤετ͸CJOEͨ͘͠ͳ͍͚Ͳड͚औΓ͍ͨ.PEFM"UUSJCVUF͸CJOEJOHGBMTFΛࢦఆ͢Δ
  4. ը໘͔ΒͷೖྗΛड͚औΔओཁͳํ๏ !.PEFM"UUSJCVUF w 1SPT w ෳࡶͳΦϒδΣΫτΛ૊Έ্ཱͯͨͰϦΫΤετΛόΠϯυ Մೳ FYόϦσʔγϣϯʹඞཁͳσʔλͳͲ΋औಘͯ͠อ࣋ ͓͚ͯ͠Δʣ w

    $POT w ίϯτϩʔϥͷ࣮૷୯Ґʹ੍໿͕ൃੜ͢Δ
 !.PEFM"UUSJCVUF͕෇͍ͨϝιου͸ͦͷίϯτϩʔϥ΁ͷ ϦΫΤετ͝ͱʹৗʹʹ࣮ߦ͞ΕΔ
 ˞ઃܭ͸ΫϦʔϯʹͳΔͷͰΉ͠ΖϝϦοτ
  5. ิ଍7BMVF0CKFDUͰͷडऔ public class Name {
 @NotEmpty(message = "ࢯ໊ೖྗ͍ͯͩ͘͠͞")
 @Length(max =

    30, message = "ࢯ໊͸30จࣈ·ͰͰೖྗ͍ͯͩ͘͠͞")
 String value = "";
 
 public Name(String value) {
 this.value = value;
 }
 
 @Override
 public String toString() {
 return value;
 }
 } 4QSJOH.7$͸3FRVFTU1BSBN1BUI7BSJBCMF.PEFM"UUSJCVUFશͯͷ৔߹Ͱɺ 4JOHMF4USJOH$POTUSVDUPS ͕͋Ε͹࢖ͬͯ͘ΕΔ $POT w UIZNFMFBGͷUIpFMEͱόΠϯυ͢Δʹ͸UP4USJOHͰWBMVFͦͷ΋ͷΛฦ࣮͢૷͕ ඞཁʹͳΔ
  6. ೖྗͷόΠϯυΛΧελϚΠζ͢Δ %JSFDU'JFME"DDFTT @ControllerAdvice(basePackages =“com.example.presentation”)
 public class DirectFieldAccessAdvice {
 @InitBinder
 public

    void initBinder(WebDataBinder binder) {
 binder.initDirectFieldAccess();
 binder.setAllowedFields("to be specified");
 }
 } ᶃ lDPNFYBNQMFQSFTFOUBUJPOzύοέʔδʹ͋Δશ$POUSPMMFSʹద༻͢Δ "EWJDFͷதͰɺೖྗΛΦϒδΣΫτʹόΠϯυ͢ΔࡍɺTFUUFSΛܦ༝ͤͣ ௚઀ϑΟʔϧυʹηοτ͢ΔΑ͏ʹઃఆ ᶄ ҙਤ͠ͳ͍ϑΟʔϧυ஋ͷվ᜵Λ๷͙ͨΊແޮͳ஋Λઃఆ͠ɺͦΕͧΕͷί ϯτϩʔϥͰݸผʹઃఆΛ্ॻ͖͢Δ ᶃ ᶄ
  7. ೖྗͷόΠϯυΛΧελϚΠζ͢Δ %JSFDU'JFME"DDFTT @InitBinder
 void initBinder(WebDataBinder binder) {
 binder.setAllowedFields("mailAddress", "name");
 }

    public class Employee {
 EmployeeNumber number;
 
 MailAddress mailAddress;
 
 Name name;
 
 public EmployeeNumber number() {
 return number;
 }
 } 1045NBJM"EESFTTlYYY!FYBNQMFDPNzOBNFlTFJKJLBXBLBNJz ίϯτϩʔϥͰڐՄ͢ΔϑΟʔϧυΛઃఆ CJOE
  8. ೖྗͷόΠϯυΛΧελϚΠζ͢Δ %JSFDU'JFME"DDFTT w 1SPT w HFUUFSTFUUFSΛແͤͯ͘ϞσϧͷϊΠζ͕ݮΔ w όΠϯυ͢ΔϑΟʔϧυ͕ίϯτϩʔϥຖʹ໌֬Ͱҙਤ͠ͳ͍վ᜵ʹ ରͯ҆͠શ w

    $POT w ίϯτϩʔϥຖʹڐՄ͢ΔϑΟʔϧυΛઃఆ͢Δඞཁ͕͋Δ w #FBO7BMJEBUJPOͱͷ૬ੑʹ೉ ޙड़ 
 ௥هਖ਼֬ʹ͸4QSJOH.7$্Ͱ#FBO7BMJEBUJPOͱ5IZNFMFBGΛ૊Έ߹ Θͤͨ࣌ʹ໰୊͕͋Δɻ#FBO7BMJEBUJPO୯ମͰ͸໰୊͕ͳ͍ɻ
  9. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ ྫࣾһͷ$36%ͷ৔߹ొ࿥ @Controller
 @RequestMapping("/employees/registration")
 public class EmployeeRegistrationController {
 @ModelAttribute("employee")


    Employee employee() {
 return service.prototype();
 }
 
 @GetMapping
 public String show() {
 return "employee-registration";
 }
 
 @PostMapping
 public String register(@Valid @ModelAttribute("employee") Employee employee,
 BindingResult bindingResult) {
 if (bindingResult.hasErrors()) return show();
 Employee registered = service.register(employee);
 return String.format("redirect:/employees/%s", registered.number());
 }
 
 @InitBinder
 void initBinder(WebDataBinder binder) {
 binder.setAllowedFields("mailAddress", "name");
 }
 }
  10. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ ྫࣾһͷ$36%ͷ৔߹ @Controller
 @RequestMapping("/employees/{number}")
 public class EmployeeController {
 


    @ModelAttribute("employee")
 public Employee employeeOf(@PathVariable("number") EmployeeNumber number) {
 return service.employeeOf(number);
 }
 
 @GetMapping
 public String show() {
 return "employee-detail";
 } 
 }
  11. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ ྫࣾһͷ$36%ͷ৔߹ฤू @Controller
 @RequestMapping("/employees/{number}/edit")
 public class EmployeeEditController {
 @ModelAttribute("employee")


    public Employee employeeOf(@PathVariable("number") EmployeeNumber number) {
 return service.employeeOf(number);
 }
 
 @GetMapping
 public String show() {
 return "employee-edit";
 }
 
 @PostMapping
 public String modify(@Valid @ModelAttribute("employee") Employee employee,
 BindingResult bindingResult) {
 if (bindingResult.hasErrors()) return show();
 service.modify(employee);
 return String.format(“redirect:/employees/%s", employee.number());
 }
 
 @InitBinder
 void initBinder(WebDataBinder binder) {
 binder.setAllowedFields("mailAddress", "name");
 }
 }
  12. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ ྫࣾһͷ$36%ͷ৔߹ @Controller
 @RequestMapping("/employees/{number}/delete")
 public class EmployeeEditController {
 @ModelAttribute("employee")


    public Employee employeeOf(@PathVariable("number") EmployeeNumber number) {
 return service.employeeOf(number);
 }
 
 @GetMapping
 public String show() {
 return "employee-delete";
 }
 
 @PostMapping
 public String delete( @ModelAttribute(“employee”) Employee employee ) {
 if (bindingResult.hasErrors()) return show();
 service.delete(employee);
 return String.format(“redirect:/employees/%s", employee.number());
 }
 }
  13. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ 1SPT w ͋Δը໘ͷ࣋ͭػೳʹूதͰ͖Δ w !.PEFM"UUSJCVUFʹΑΔσʔλόΠϯσΟϯάͱ૬ੑ͕ྑ͍ w %JSFDU'JFME"DDFTTͱ΋૬ੑ͕ྑ͍ $POT

    w ୯ҰͷϦιʔεʹର͢ΔΦϖϨʔγϣϯ͕෼ׂ͞ΕΔͷͰɺ1BUI7BSJBCMFͰϦιʔ εΛදݱ͍ͯ͠Δ৔߹ಉ͡63-Λෳ਺ίϯτϩʔϥʹهड़͢Δ͜ͱʹͳΔ͕ɺΞϊ ςʔγϣϯʹࢦఆ͢Δจࣈྻ͸੩తͰ͋Δඞཁ͕͋ͬͯɺTUBUJDͷจࣈྻఆ਺ఆٛ ͱจࣈྻ࿈͔݁͠ํ๏͕ͳ͍ w ಉ͡!.PEFM"UUSJCVUFએݴ͕ෳ਺ͷίϯτϩʔϥʔʹࢄΒ͹Γ͕ͪ
  14. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ $POTͷ!.PEFM"UUSJCVUFͷॏෳΛ!$POUSPMMFS"EWJDFͰҰݩԽ͢Δ @ControllerAdvice(basePackageClasses = EmployeeAdvice.class)
 public class EmployeeAdvice {


    public static final String BASE_URL="/employees/{number}";
 public static final String EMPLOYEE="employee";
 
 @ModelAttribute(EMPLOYEE)
 Employee employeeOf(@PathVariable("number")EmployeeNumber employeeNumber) {
 return service.employeeOf(employeeNumber);
 }
 
 EmployeeService service;
 public EmployeeAdvice(EmployeeService service) {
 this.service = service;
 }
 } $POUSPMMFS"EWJDF 1BUI7BSJBCMF΁ͷΞΫηε΋ҰݩԽͰ͖ͨͷͰ #"4&@63-Λఆ਺ͱͯ͠1BUI7BSJBCMFΛ͜ͷ"EWJDFʹӅṭ͍ͯ͠Δ
  15. ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ $POTͷ!.PEFM"UUSJCVUFͷॏෳΛ!$POUSPMMFS"EWJDFͰҰݩԽ͢Δ @Controller
 @RequestMapping(EmployeeAdvice.BASE_URL + "/edit")
 public class EmployeeEditController

    {
 @GetMapping
 public String show() {
 return "employee-edit";
 }
 
 @PostMapping
 public String modify(
 @Valid @ModelAttribute(EmployeeAdvice.EMPLOYEE) Employee employee,
 BindingResult bindingResult
 ) {
 if (bindingResult.hasErrors()) return show();
 service.modify(employee);
 return String.format(“redirect:/employees/%s", employee.number());
 }
 
 @InitBinder
 void initBinder(WebDataBinder binder) {
 binder.setAllowedFields("mailAddress", "name");
 }
 } ฤू จࣈྻఆ਺ͱจࣈྻ࿈݁͸ෆ֨޷͕ͩ ΍ΓΑ͏͕ͳ͍ɻɻɻΞϊςʔγϣϯ ͷਏΈ
  16. @Controller
 @RequestMapping(EmployeeAdvice.BASE_URL + "/delete")
 public class EmployeeDeleteController {
 @GetMapping
 public

    String show() {
 return "employee-delete";
 }
 
 @PostMapping
 public String delete( @ModelAttribute(EmployeeAdvice.EMPLOYEE) Employee employee ) {
 service.delete(employee);
 return "redirect:/employees";
 }
 } ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ $POTͷ!.PEFM"UUSJCVUFͷॏෳΛ!$POUSPMMFS"EWJDFͰҰݩԽ͢Δ ࡟আ
  17. @Controller
 @RequestMapping(EmployeeAdvice.BASE_URL)
 public class EmployeeController {
 @GetMapping
 public String showDetail()

    {
 return "employee-detail";
 }
 } ίϯτϩʔϥͷ࣮૷୯Ґ )5.-ςϯϓϨʔτ୯Ґ $POTͷ!.PEFM"UUSJCVUFͷॏෳΛ!$POUSPMMFS"EWJDFͰҰݩԽ͢Δ ৄࡉ γϯϓϧʂ
  18. ঢ়ଶͷ࣋ͪճΓํ๏ $PPLJF 1SPT w ୯७ʹεέʔϧ͢Δ w 0CKFDU4FSJBMJ[FSʹཔΒͣɺ4FSJBMJ[BCMFͷ࣮૷΋ෆཁ $POT w αΠζ੍ݶ

    ʙ,  w վ᜵ʹऑ͍ ॺ໊ΛؚΊΔࣄͰճආ͸Մೳ  w 4FTTJPOͷΑ͏ͳखܰ͞ΛٻΊΔͱ࡞ΓࠐΈ͕গ͠ඞཁʹͳΔ
 ࠷ۙ͸+BDLTPOͰ+40/ʹม׵ͯ͠63-Τϯίʔυͨ͠΋ͷΛ$PPLJFͱ͠ ͯൃߦ͢Δ΍ΓํΛ޷ΜͰ࢖͍ͬͯΔ
  19. ঢ়ଶͷ࣋ͪճΓํ๏ %#ʹอଘ͠Ϧιʔεͱͯ͠*%Λൃߦ ঢ়ଶΛ%#ʹอଘͯ͠*%Λൃߦɻͦͷ*%Λ63- 1BUI7BSJBCMF Ͱ࣋ͪճΔ
 ࢀߟ3&45'VM8FCαʔϏεIUUQTXXXPSFJMMZDPKQCPPLT ͷʮτϥϯβΫγϣϯΛϦιʔεͱͯ͠ൃߦ͢Δʯͱ͍͏هड़͔ΒΞΠσΞΛഈआ 
 
 1SPT

    w ར༻ऀ͕ɺ్தͰσόΠεΛ࣋ͪସ͑ͨ৔߹ʹ΋63-͑͞Θ͔Ε͹ঢ়ଶΛҾ͖ ܧ͛Δ $POT w 63-ʹอଘͨ͠ঢ়ଶͷ*%ΛຒΊࠐΉͷͰվ᜵ʹऑ͍ʢ+85ͱͯ͠ຒΊࠐΉͳ ͲͰճආ͸Մೳʣ w खܰ͞͸ͳ͘ɺ࡞ΓࠐΈͷੈք
  20. 3&45"1*Ͱ΋ %JSFDU'JFME"DDFTT !3FTU$POUSPMMFSͰ+TPOϕʔεͷ3FTU"1*Λ࡞Δ࣌ Ͱ΋%JSFDU'JFME"DDFTTʹͯ͠HFUUFSTFUUFSΛແ͘ ͤ·͢ɻ@Configuration
 public class ObjectMapperConfig {
 


    @Bean
 public ObjectMapper directFieldAccessObjectMapper() {
 ObjectMapper mapper = new ObjectMapper();
 mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NON_PRIVATE);
 mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
 mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
 return mapper;
 }
 } 4QSJOH#PPUͳΒ͜ΕͰ