본문 바로가기

Web/spring

[Spring framework Web MVC docs] 1.3.4 Model

728x90

틀린 해석이 있다면 알려주세요

 


 

@ModelAttribute 사용

 

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-modelattrib-method-args

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl

docs.spring.io

 

@RequestMapping 을 생성하거나, WebDataBinder 를 통해 Object에 접근

메서드 호출 전에 모델 초기화 -> @Controller, @ControllerAdvice

 

@RequestMapping 에는 모델 속성이 있음

 

 

controller 는 여러개의 @ModelAttribute 를 가질 수 있음

컨트롤러간 공유도 가능함 -> @ControllerAdvice 를 이용

 

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-controller-advice

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl

docs.spring.io

 

 

model 추가

@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
    model.addAttribute(accountRepository.findAccount(number));
    // add more ...
}

 

 

attribute 하나만 추가하기

@ModelAttribute
public Account addAccount(@RequestParam String number) {
    return accountRepository.findAccount(number);
}

 

이름이 명시되어있지 않으면 기본 이름이 선택됨 

https://docs.spring.io/spring-framework/docs/6.0.4/javadoc-api/org/springframework/core/Conventions.html

 

Conventions (Spring Framework 6.0.4 API)

Determine the conventional variable name for the given parameter taking the generic collection type, if any, into account. Determine the conventional variable name for the return type of the given method, taking the generic collection type, if any, into ac

docs.spring.io

 

 

 

 

 

이름 정의 하는 방법

@GetMapping("/accounts/{id}")
@ModelAttribute("myAccount")
public Account handle() {
    // ...
    return account;
}

 

 

728x90