#port 수정
server.port=9090
#한글
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
#thyme
spring.thymeleaf.cache=false
기본적인 셋팅읋 해주었다.
<a href="/hello">Hello</a>
를 통해 "/hello" 로 요청을 보내게 하자.
JSP때 했던 것 처럼 먼저 프론트 컨트롤러가 필요하다.
여기서는 HelloController.java를 만든다.
@GetMapping("hello")
public String hello(Model model) {
System.out.println("hello mapping");
model.addAttribute("data", "hello mapping!!!");
return "hello";
@GetMapping으로 요청받은 url을 파싱해온다.
data하나 넘길꺼니까 Model model 을 파라미터로 넣어준다.
return "hello"; 이부분은 trmplates폴더 아래에 hello.html이라는 파일을 찾아가라는 말이다.
data에 담긴 문자열을 기존의 안녕하세요, 손님 이라는 text에 덮어쓰게 해주는 것이 thymeleaf이다.
제일 많이 쓰게 되며, th:text="~~~" 라고 쓰면 된다.
주소창에서 직접
localhost:9090/hello-mvc
라고 호출을 한다.
그러면 HelloController.java에서
@GetMapping("/hello-mvc")
public String helloMvc(@RequestParam(value="name", required=false, defaultValue = "required value") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
프론트 컨트롤러를 통해 hello-template.java를 호출할 것이다.
@Request.Param("name") : 기본 name이라는 key값을 파싱한다. 욥션을 넣어 줄 수 있다.
여기서 한발 자국 더 나아갔다.
MemberController.java파일을 만들어서 @Controller선언을 해주고
member라는 요청을 받았을 경우의 상황이다.
MemberDTO라는 클래스를 하나만들고 생성자를 통해 설정을 한다.
model에 담아 thymeleaf/member 라는 html파일로 리턴한다.
<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>
이렇게 객체에서의 정보를 *{ } 을 통해 꺼내올 수 있다.