View에 데이터 전달

  • Model 객체 사용
  @GetMapping("")
  public String list(Model model, Pageable pageable, ProjectFilter filter) {

    model.addAttribute("projectList", projectService.findAllByFilter(pageable, filter));
    model.addAttribute("filter", filter);
    return "project/list";
  }

간단히 controller 생성

 

<tr th:each="p: ${projectList}">
          <td scope="row" style="width: 50px"><a th:text="${p.id}"
                                                 th:href="|@{/project/read}/${p.id}|"></a></td>
          <td><a th:text="${p.date}" th:href="|@{/project/read}/${p.id}|"></a></td>
          <td><a th:text="${p.title}" th:href="|@{/project/read}/${p.id}|"></a></td>

model로 받아온 projectList 활용

 

 

  • @ModelAttribute("key") 활용

controller의 함수 위에 @ModelAttribute("원하는 key")를 붙여서 사용

파라미터 안에 선언해줘도 가능

 

❗ 나는 주로 객체를 활용할 듯.. ❗

+ Recent posts