redirect,thymeleaf
2024. 3. 12. 17:36ㆍDaily Codig Reminder
thymeleaf
main
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/script.js"></script>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Main.html</h1>
<img src="images/a.jpg">
</body>
</html>
main2
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Main2.html</h1>
<p th:text="'홍길동'"></p>
<p th:text="${userid}"></p>
<p th:text="${username}"></p>
<p th:text="${session.username}"></p>
<p th:text="${application.username}"></p>
</body>
</html>
main3
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>main3.html</h1>
<p th:text="#{hello}"></p>
</body>
</html>
main4
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>main4.html</h1>
<p th:text="${user.username}"></p>
<p th:text="*{user.username}"></p>
<hr>
<div th:object="${user}">
<!-- 부모자식관계여야 출력 -->
<p th:text="*{username}"></p>
<p th:text="*{age}"></p>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>main5.html</h1>
<a href = "http://localhost:8090/app/a55?username=홍길동&age=20">홍길동/20</a>
<a href="a55?username=이순신&age=10">이순신</a>
<hr>
<a th:href="@{http://localhost:8090/app/a55(username=홍길동, age=20)}">홍길동/20</a>
<a th:href="@{/a55(username=세종, age=40)}">세종/40</a>
<a th:href="@{/a55?username=세종&age=40}">세종/40</a>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>main5_1.html</h1>
<p th:text="${param.username}"></p>
<p th:text="${param.age}"></p>
</body>
</html>
main6
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>main6.html</h1>
<h3>1. 문자열 리터럴</h3>
<p th:text="'문자열을 넣기'">template file</p>
<h3>2. 숫자 리터럴</h3>
<p>this year is <span th:text="${2013}">1111</span>.</p>
<p>In two years, it will be <span th:text="${2013+2}">1111</span>.</p>
<h3>3. 불린 리터럴: 태그의 사용 여부 결정</h3>
<div th:if="${user.username == '홍길동'}">이름은 홍길동 (thymeleaf 가 처리)</div>
<div th:if="${user.username == '홍'}">이름은 홍길동 (springEL 가 처리)</div>
<div th:if="${user.username != 'aaa'}">이름은 aaa 아님</div>
<hr>
<div th:if="${user.age != null}">null 문자 사용 가능</div>
<div th:if="${user.username != null}">null 문자 사용 가능</div>
<h3>4. null 리터럴</h3>
<div th:if="${user.username == null}">null 문자 사용 가능1</div>
<div th:if="${user.username != null}">null 문자 사용 가능2</div>
<h3>5. 텍스트 추가 및 리터럴 대체</h3>
<div th:text="'the name of the user is ' + ${user.username}">아무개</div>
<div th:text="|the name of the user is ${user.username} hello/|">리터럴 대체</div>
</body>
</html>
main7
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.xxx{
color: red;
}
.yyy{
color: blue;
}
</style>
</head>
<body>
<h3>1.조건 : th:if</h3>
<div th:if="${user.getAge()==20}">user 나이 20살(springEL)</div>
<div th:if="${user.getAge()}==20">user 나이 20살(thmeleaf)</div>
<h3>2. 3항연산자</h3>
<div th:class="${user.getAge()==20}?'xxx':'yyy'">hello</div>
<div th:if="${user.getAge()==20}?'20살임':'20살아님'">hello</div>
<h3>3. switch~ case?</h3>
<div th:switch="${user2.username}">
<p th:case="'hong'">hong2</p>
<p th:case="#{roles.manager}">manager</p>
<p th:case="*">default 역할</p>
<h3>4. 반복: th:each</h3>
<table border="1">
<tr><td>이름</td><td>나이</td></tr>
<tr th:each ="user : ${userList}">
<td th:text = "${user.username}"></td>
<td th:text = "${user.age}"></td>
</tr>
</table>
<h3>4. 반복: th:each, iterState</h3>
<table border="1">
<tr><td>번호</td><td>이름</td><td>나이</td></tr>
<tr th:each ="user, iterState : ${userList}">
<td th:text = "${iterState.index}+' : '+${iterState.count}"></td>
<td th:text = "${user.username}"></td>
<td th:text = "${user.age}"></td>
</tr>
</table>
</div>
</body>
</html>
main8
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/style.css}" type="text/css">
</head>
<body>
<h1>속성값 변경 : th:attr</h1>
<img th:attr="src=@{/images/a.jpg}, width=${imgWidth}, height=${imgHeight}"><br>
<img th:src ="@{/images/a.jpg}" th:width="${imgWidth}" th:height="${imgHeight}"><br>
<p th:class = "${xyz}">hello</p>
<p th:attr = "class=${xyz}">hello2</p>
아이디: <input type="text" name="userid" th:value="${user.username}">
아이디: <input type="text" name="userid" th:attr="value=${user.username}">
<hr>
<form th:action="@{/a2}">
a2 로 전송되는 id<input type="text" name="userid" th:value="${user.username}">
<input th:attr="type='submit' , value='전송'">
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:fragment="menu">
top.html fragment(menu) 로그인|회원가입 : <span th:text="${username}"></span>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:fragment="menu">
include.html|로그인|회원가입 : <span th:text="${username}"></span>
</div>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery <span th:text="${username}"></span>
</div>
<div th:fragment="xyz">
© 2011 The Good Thymes ............... <span th:text="${username}"></span>
</div>
</body>
</html>
main9
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>조각모음</h1>
<h3>top포함 th:insert</h3>
<div th:insert="fragments/include::menu"></div>
</body>
<h3>copy포함 th:insert</h3>
<div th:insert="fragments/include::copy"></div>
<h3>copy 변경 th:insert</h3>
<div th:replace="fragments/include::xyz"></div>
</body>
</body>
</html>
main10
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script th:inline="javascript">
/* <![CDATA[ */
const mesg = [[${message}]];
console.log(mesg);
if(mesg){
alert(mesg);
}
</script>
</head>
<body>
</body>
</html>
package com.example.controller;
@Controller
public class MainController {
@Autowired
ServletContext application;
//기본
@GetMapping("/")
public String m() {
return "main";
}
//2. 바인딩 - ${...}
@GetMapping("/a2")
public String a2(Model m , HttpSession session, String userid) {
System.out.println("a2 userid "+userid);
m.addAttribute("username","request홍길동1");
session.setAttribute("username", "session 홍길동");
application.setAttribute("username", "application홍길동");
return "main2";
}
//3. 바인딩 - #{...}
@GetMapping("/a3")
public String a3(Locale xxx) {
System.out.println("current locale xxx: "+xxx);
return "main3";
}
//4. 객체 속성값 직접 접근 ..., th:object=${ 객체 키값} , *{속성명}
@GetMapping("/a4")
public ModelAndView a4() {
ModelAndView mav = new ModelAndView();
mav.addObject("user", new User("홍길동", 20));
mav.setViewName("main4");
return mav;
}
//5. 링크 @{...} , request.getContextPath == c: url 같은 역할
// 기존 aaa?userid=abcd&passwd=1234
//aaa(userid=abcd, passwd=1234) 로 사용가능
@GetMapping("/a5")
public String a5() {
return "main5";
}
//5. 링크 @{...}
@GetMapping("/a55")
public String a55(String username, int age) {
System.out.println("/a55"+username+"\t"+age);
return "main5_1";
}
//6. 리터럴(literal): 1) "' ' +${.....}",
//2)"| ${.....} |"
@GetMapping("/a6")
public String a6(Model m) {
m.addAttribute("user", new User("홍길동", 20));
return "main6";
}
@GetMapping("/a7")
public String a7(Model m) {
m.addAttribute("user",new User("홍길동", 20) );
m.addAttribute("user2",new User("hong", 20) );
List<User> list=
Arrays.asList(new User("홍길동1", 20),
new User("홍길동2", 30),
new User("홍길동3", 40)
);
m.addAttribute("userList", list);
return "main7";
}
@GetMapping("/a8")
public String a8(Model m) {
m.addAttribute("user", new User("홍길동", 20) );
m.addAttribute("imgWidth",100);
m.addAttribute("imgHeight",200);
m.addAttribute("xyz","kkk2");
return "main8";
}
@GetMapping("/a9")
public String a9(Model m) {
m.addAttribute("username","홍길동");
return "main9";
}
@GetMapping("/a10")
public String a10(Model m) {
m.addAttribute("message","홍길동");
return "main10";
}
}
'Daily Codig Reminder' 카테고리의 다른 글
ECMA (1) | 2024.03.12 |
---|---|
thymeleaf, external_img (0) | 2024.03.12 |
boot-mybatis (0) | 2024.03.12 |
boot-database (0) | 2024.03.11 |
component-scan (0) | 2024.03.11 |