package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "spring!!");
return "hello";
}
**@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}**
}
HelloController.java파일의 helloMvc메서드의 return 값이 hello-template이므로, 이 메서드를 이용해서 띄우고 싶은 html 파일을 만들어준다.
<aside> 💡 Thymleaf의 장점! html 파일의 절대경로를 주소창에 입력하면 껍데기를 볼 수 있음
</aside>
hello-template.html 우클릭 > Copy Path/Reference… 클릭
Absolute Path 클릭
껍데기 확인
페이지 소스 확인
에러남..!!!
2023-02-06T22:20:41.605+09:00 WARN 9748 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: **Required request parameter 'name' for method parameter type String is not present**]
ctrl+P > 속성확인 > required() 속성이 있는 것 확인
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "spring!!");
return "hello";
}
// 사실 required는 default 값으로 true임.
// name이라는 값을 요청하는 것이 항상 true라는 것.
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(**name = "name", required = true**) String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
?뒤에 name=spring! 이라고 되어있으면 “spring!”이라는 값을 HelloController.java의 메서드에게 넘겨준 것이고, 그 값을 hello-template.html으로 출력하게 된다.
느낌표가 많아진 것을 확인할 수 있음