Java/Spring Boot

Spring Boot 설정 및 기초 - AWS 풀스택 과정 76일차

awspspgh 2024. 11. 13. 09:25
목차
1. 설정
2. 기초

 

1. 설정

출처 : https://blog.naver.com/momonocha/223657634539

 

2. 기초

◈ application.properties

spring.application.name=demo

server.port=8089
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

 

 index.html

<!DOCTYPE html>
<html lang="en"
  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>${msg}</h1>
    <h1 th:text="${msg}"></h1>
    <h1>[[${msg}]]</h1>
    <hr>
    <h1>[[${list}]]</h1>
<h1 th:text="${list}"></h1>

<!-- th:each를 이용하여 반복값을 출력 -->

    <ul>
        <li th:each="str:${list}" th:text="${str}"></li>
    </ul>

    <hr>

    <ul>
        <li th:each="str:${list}">[[${str}]]</li>
    </ul>

    <ul>
        <li th:each="str, status:${list}">
            [[${status.count}]] --- [[${str}]]
        </li>
    </ul>

    <hr>

    <ul>
        <li th:each="str, status:${list}">
            <span th:if="${status.odd}">ODD --- [[${str}]]</span>
            <span th:unless="${status.odd}">EVEN(unless) --- [[${str}]]</span>
        </li>
    </ul>

    <hr>

    <ul>
        <li th:each="str, status:${list}">
            <span th:text="${status.odd} ? 'odd - ' + '${str}' : 'even - ' + '${str}'"></span>
        </li>
    </ul>

    <hr>

    <ul>
        <li th:each="str, status:${list}">
            <th:block th:switch="${status.index % 3}">
            <span th:case="0">0 -- [[${str}]]</span>
            <span th:case="1">1 -- [[${str}]]</span>
            <span th:case="2">2 -- [[${str}]]</span>
            </th:block>
        </li>
    </ul>

    <hr>

<th:block th:with="num1=${10}, num2=${20}">
    <span>[[${num1+num2}]]</span>
</th:block>

</body>
</html>

 

 ex01.html

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>Controller에서 보낸 리스트를 인라인 방식으로 출력</h1>
[[${list}]]
<ul>
  <li th:each="str:${list}" th:text="${str}"></li>
</ul>

<a href="/ex02?name=hong&age=20">Go To /ex02</a> <br>
<a th:href="@{/ex02(name='kim', age='30')}">Go To /ex02</a>

</body>
</html>

 

 ex02.html

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1> EX02 HTML page</h1>
<h1>[[${param.name}]]</h1>
<h1>[[${param.age}]]</h1>
</body>
</html>

 

 TestVO.java

package com.example.demo;

import lombok.*;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class TestVO {

    private String p1, p2, p3;
}

 

 TestController.java

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Controller
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/")
    public String index(Model m){
        m.addAttribute("msg","Test Thymeleaf World!!");
        List<String> list = Arrays.asList("aaa","bbb","ccc","ddd","eee","fff");
        m.addAttribute("list", list);
        return "index";
    }

    /* /ex01 => 리스트를 /ex/ex01.html 전송*/
    @GetMapping("/ex01")
    public String ex01(Model m){
        List<String> list = Arrays.asList("영이","철수","영철","길동","순신");
        m.addAttribute("list", list);
        return "/ex/ex01";
    }

    @GetMapping("/ex02")
    public String ex02(@RequestParam("name")String name, @RequestParam("age")int age, Model m){
        log.info("name : {}", name);
        log.info("age : {}", age);
        TestVO tvo = new TestVO("a","b","c");
        m.addAttribute("tvo",tvo);
        return "/ex/ex02";
    }
}

 

▷ 출력