변수 - SpringEL

변수를 사용할 때는 변수 표현식을 사용한다. ${...}

  • 컨트롤러

@GetMapping("/variable")
    public String variable(Model model) {
        User userA = new User("userA", 20);
        User userB = new User("userB", 30);

        List<User> list = new ArrayList<>();
        list.add(userA);
        list.add(userB);

        Map<String, User> map = new HashMap<>();

        map.put("userA", userA);
        map.put("userB", userB);

        model.addAttribute("user", userA);
        model.addAttribute("users", list);
        model.addAttribute("userMap", map);

        return "basic/variable";
    }

@Data
static class User{
    private String username;
    private int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }
}
  • HTML

img.png

기본적으로 프로퍼티 접근법을 사용하여 데이터를 조회한다.

th:with를 사용하여 지역 변수로 선언할 수 있다. 선언한 태그 안에서만 사용할 수 있다.

Last updated