설정을 어노테이션을 이용하여 대신한다.
server.port=9090
spring.thymeleaf.cache=false
- spring.thymeleaf.cache=false
개발을 할 때에는 false로 해 주는 것이 재시작 없이 새로고침만으로 반영
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
System.out.println("controller 도착");
model.addAttribute("data", "hello!!!!");
return "hello";
}
// 요청 URL -> http://localhost:9090/hello-mvc?name=SpringMVC
// @GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String param, Model model ) {
System.out.println("controller 도착");
System.out.println(param);
model.addAttribute("name",param);
return "hello-template";
}
/* @RequestParam
* - required : 파라미터값 필수 여부, true -> 필수(default), false - 필수 아님
* - defaultValue : 파라미터 값이 없을 경우 기본으로 들어갈 값
*/
@GetMapping("hello-mvc")
public String helloMvc2(@RequestParam(value="name", required = false,
defaultValue = "required test") String param, Model model ) {
model.addAttribute("name",param);
return "hello-template";
}
}
html xmlns:th="http://www.thymeleaf.org"
- 타임리프의 th속성을 사용하기 위해 선언된 네임스테이스이다.
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
- 값을 안넣었을 경우
- 값을 넣었을 경우
public class MemberDTO {
private int no;
private String name;
private String phone;
public MemberDTO(int no, String name, String phone) {
super();
this.no = no;
this.name = name;
this.phone = phone;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
@Controller
public class MemberController {
// member getmapping이 이루어지는 메서드 생성
// view : thymeleaf/member -> html파일까지 생성할 것
// @RequestMapping -> get, post방식 상관없이 가능
@RequestMapping("member")
public String getMember(Model model) {
MemberDTO member = new MemberDTO(1, "자바학생", "01012345678");
model.addAttribute("member",member);
return "thymeleaf/member";
}
}
<body>
<table border="1">
<tr>
<th>번호</th>
<th>이름</th>
<th>전화번호</th>
</tr>
<tr th:object=${member}>
<td><span th:text=*{no}></span></td>
<td><span th:text=*{name}></span></td>
<td><span th:text=*{phone}></span></td>
</tr>
</table>
</body>