Teacher.java
package com.oraclejava.springboot;
import java.util.ArrayList;
import java.util.List;
public class Teacher {
private String gender; // 성별(M F)
private boolean isActive; // TRUE활동중 / FALSE 은퇴
private List<String> courses = new ArrayList<>(); // 강의 가능 과목
private String additionalSkills; // 기타 스킬
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
public String getAdditionalSkills() {
return additionalSkills;
}
public void setAdditionalSkills(String additionalSkills) {
this.additionalSkills = additionalSkills;
}
}
TeacherController.java
package com.oraclejava.springboot;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TeacherController {
@RequestMapping(value="/teacher", method=RequestMethod.GET)
public ModelAndView showTeacher(ModelAndView mav) {
Teacher teacher = new Teacher();
teacher.setGender("M");
teacher.setActive(true);
teacher.setCourses(List.of("자바", "닷넷", "오라클"));
mav.setViewName("teacherView");
mav.addObject("teacher", teacher);
return mav;
}
}
teacher.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/style.css}">
</head>
<body>
<h1>Teacher View</h1>
<table th:object="${teacher}">
<tr><th>성별</th>
<td>
<span th:if="*{gender == 'F'}">여성</span>
<span th:else="*{gender == 'F'}">남성</span>
</td>
</tr>
<tr><th>활동여부</th><td th:text="*{isActive} ? '활동중' : '은퇴'"></td></tr>
<tr><th>부가스킬</th><td th:text="*{additionalSkills} ?: '없음'"></td></tr>
<tr><th>강의가능과목</th>
<td th:switch="*{#lists.size(courses)}">
<span th:case="0">강의가능과목 없음</span>
<span th:case="1" th:text="*{courses[0]}"></span>
<div th:case="*">
<div th:each="course:*{courses}" th:text="${course}"/>
</div>
</td>
</tr>
</table>
</body>
</html>
'spring' 카테고리의 다른 글
Request와 Router로 분리 (0) | 2023.07.23 |
---|---|
gradle bootRun (0) | 2023.06.27 |
Thymeleaf CSS 처리 (0) | 2023.06.26 |
Spring Thymeleaf th:object 식 (0) | 2023.06.26 |
spring mvc 설정 (0) | 2023.06.20 |