20220527 TIL

Don Lee·2022년 5월 29일
0

EpiTIL

목록 보기
17/24


✅ 20번째 줄에 name과 age를 + 로 지정하지 않았는데, 18번째 name과 age가 어떻게 이쁘게 쏘옥 들어가는지 궁금. `"Hello, @PathVariable.
name = " + name, + "age" + age;` 이래야 할 거 같은데. 이정찬 님 해설:`**.format`의 특징이다. `%`와 `변수`의 순서를 맞춰 설정을 하면 이쁘게 잘 들어간다.**

SpringBoot에서 static폴더와 template폴더의 차이

20220527 내가 보기에는 정적(static)파일이 있는 곳과 동적(templates)파일이 모여있다는 차이가 있다고 생각함.

demo
  |+-src/main
  |    +-java
  |    +-resources
  |         static
  |         templates
  |         application.properties
  |-pom.xml
  • templates 스프링이 계속 버전이 올라가면서 view 엔진이 JSP 대신 thymeleaf로 바뀌었습니다. templates폴더는 thymeleaf의 파일들을 두는 곳입니다.
  • static static 폴더는 content들을 두는 곳입니다. 보통 css나 js를 두곤합니다./static 을 이용해서 웹에서 호출할 수도 있습니다.

Model

  • 토비의 스프링 1144p 다른 애노테이션이 있지 않다면 java.util.Map 그리고 스프링의 org.springframework.ui.Modelorg.springframework.ui.ModelMap 타입의 파라미터는 모두 모델정보를 담는 데 사용할 수 있는 오브젝트가 전달된다. 모델을 담을 맵은 메소드 내에서 직접 생성할 수도 있지만 그보다는 파라미터로 정의해서 핸들러 어댑터에서 미리 만들어 제공해주는 것을 사용하면 편리하다. ModelModelMap은 모두 addAttribute() 메소드를 제공해준다. 일반적인 맵의 put()처럼 이름을 지정해서 오브젝트 값을 넣을 수도 있고,자동 이름 생성 기능을 이용한다면 오브젝트만 넣을 수도 있다. 예를 들어 다음과 같이 ModelMapUser 타입의 오브젝트를 넣는다면 타입정보를 참고해서 "user"라는 모델 이름이 자동으로 부여 된다.
    @RequestMapping(...)
    public void hello(ModelMap model) {
    	User user = new User(1z "Spring");
    	model.addAttribute(user)-> addAttributeCuser; user)와 동일 
    }
    ModelMapModel의 addAllAttribute() 메소드를 사용하면 Collection에 담긴 모든 오브젝트를 자동 이름 생성 방식을 적용해서 모두 모델로 추가해준다.
  • https://dev-coco.tistory.com/100 Model 객체는 Controller 에서 생성된 데이터를 담아 View 로 전달할 때 사용하는 객체이다. ※ Servletrequest.setAttribute() 와 비슷한 역할을 함. addAttribute("key", "value") 메서드를 이용해 view에 전달할 데이터를 key, value형식으로 전달할 수 있다.
    package com.mystudy.coco.controller;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
     
    @Controller
    public class TestController {
     
        @GetMapping("/test")
        public void testMethod(Model model) {
            String msg = "model test";
     
            model.addAttribute("value", msg);
        }
    }
    <h2> Test Method : ${value} </h2>
    https://velog.io/@han_been/서블릿-컨테이너Servlet-Container-란
profile
쾌락코딩

0개의 댓글