시그마 삽질==six 시그마

Thymeleaf 사용법 1탄 본문

프로그래밍/theamleaf

Thymeleaf 사용법 1탄

Ethan Matthew Hunt 2020. 4. 29. 19:08

Spring 진영에서 밀어주고 있는 Thymeleaf는 server side 템플릿 엔진이다.

 

 

 

 

스프링부트 사용시 하단 추가

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

 

@Data
public class Person {

    private String name;
    private int age;
    private int height;
    private List<Car> carList;


    public Person(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.height = builder.height;
        this.carList = builder.carList;
    }

    public static class Builder {
        private String name;
        private int age;
        private int height;
        private List<Car> carList;

        public Builder(String name) {
            this.name = name;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public Builder height(int height) {
            this.height = height;
            return this;
        }

        public Builder car(List<Car> carList) {
            this.carList = carList;
            return this;
        }

        public Person build() {
            return new Person(this);
        }
    }
}

 

 

@Data
public class Car {
    String name;

    public Car(String name) {
        this.name = name;
    }
}

 

 

@RequestMapping("/")
@Controller
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String hello(Model model) {
        List<Person> list= Arrays.asList(
                new Person.Builder("정우성").age(30).height(180).car(Arrays.asList(new Car("소나타"),new Car("아반테"),new Car("넥소"))).build()
                ,new Person.Builder("원빈").age(19).height(181).car(Arrays.asList(new Car("소나타"),new Car("아반테"),new Car("BMW"))).build()
                ,new Person.Builder("이정").age(22).height(180).car(Arrays.asList(new Car("아반테"),new Car("그랜져"),new Car("페라리"))).build()
                );
        Person person=new Person.Builder("장동건").age(30).build();
        model.addAttribute("list", list);
        model.addAttribute("person", person);
        return "hi";
    }
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Hello Thymeleaf</title></head>
<body>

    <div th:text="'Test name: ' + ${person.name} + '님'"></div>
    <div th:text="'Test age: ' + ${person.age} + '님'"></div>

    <table border="1">
        <th>이</th>
        <th>나이</th>
        <th>키</th>
        <th>자동차</th>
        <tr th:each="item : ${list}">
            <td th:text="${item.name}"></td>
            <td th:text="${item.age}"></td>
            <td th:text="${item.height}"></td>
            <td>
                <p th:each="car,status : ${item.carList}">
                    <span th:text="'index:'+${status.index}"></span>
                    <span th:text="'count:'+${status.count}"></span>
                    <span th:text="'current:'+${status.current}"></span>
                    <span th:text="'size:'+${status.size}"></span>
                    <span th:text="'even:'+${status.even}"></span>
                    <span th:text="'odd:'+${status.odd}"></span>
                    <span th:text="'first:'+${status.first}"></span>
                    <span th:text="'last:'+${status.last}"></span>
                    <span th:text="'name:'+${car.name}"></span>
                </p>
            </td>

        </tr>

    </table>

</body>
</html>

 

 

결과

'프로그래밍 > theamleaf' 카테고리의 다른 글

Thymeleaf 사용법 3탄  (0) 2020.04.30
Thymeleaf 사용법 2탄(Thymeleaf Layout Dialect)  (0) 2020.04.29
Comments