Map<String, Object> model = new HashMap<>(); // 빈 모델 생성
String viewName = controller.process(paramMap, model); // ⓵ 컨트롤러 호출
view.render(model, request, response); // ⓶ 모델 사용
public String process(Map<String, String> paramMap, Map<String, Object> model) {
// ⓵-1: 파라미터에서 데이터 추출
String username = paramMap.get("username");
int age = Integer.parseInt(paramMap.get("age"));
// ⓵-2: 회원 저장
Member member = new Member(username, age);
memberRepository.save(member);
// ⓵-3: 모델에 데이터 추가 (핵심!)
model.put("member", member); // 원본 model 객체가 수정됨
return "save-result"; // 뷰 이름 반환
}
위에서 controller.process(paramMap, model)을 수행하면, 내부적으로 model에 "member"라는 Key와 member라는 value가 저장된다. 이때 리턴값은 String이다.
이때 파라미터 자체가 Map<String, Object> model이라는 객체이므로 "Pass by Reference"이다. 즉, 리턴값이 void더라도, 메서드를 호출한 FrontController에서의 Map<String, Object> model = new HashMap<>()의 빈 모델 안에는 해당 값이 존재한다.
이때 파라미터로 전달되는 model의 주소값과 현재 FrontController에 있는 model의 주소 값이 동일하기 때문에 model은 파라미터로 전달된 참조를 통해 원본이 수정된다.
'Java > Core' 카테고리의 다른 글
| [Stream-4][Optimization] 실무 적용과 성과 증명 전략 (0) | 2025.12.27 |
|---|---|
| [Stream-3][Optimization] Filter Overhead를 활용한 성능 개선 사례 (0) | 2025.12.27 |
| [Stream-2][Optimization] 선택도(Selectivity)와 비용(Cost)의 이해 (0) | 2025.12.27 |
| [Stream-1][Optimization] 선언적 프로그래밍과 지연 평가(Lazy Evaluation) (0) | 2025.12.27 |
| [Basic-2] 제네릭과 제네릭 메서드 (0) | 2025.08.28 |