RequestParam : 외부에서 파라메터를 전달받을 때 사용
Model : 데이터를 넘겨주기 위해 사용 (model에 담으면 view에서 사용 가능)
name(key)으로 넘겨온 파라메터값(value)을 model에 담아서 view에서 쓸 수 있도록 제어
required 부분에 Ctrl+P를 눌러보면 default가 true임을 확인, 따라서 만약 넘겨받는 파라메터값이 없으면 에러
RequestParam 에 required=false 를 주면 파라메터가 없어도 에러가 안남
파라메터는 주소줄에 ?뒤에 key=value 형식으로 작성

HelloController
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-mvc")
public String helloMvc(@RequestParam(value="name", required = false) String name, Model model){
model.addAttribute("name", name);
return "hello-template";
}
}
hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
주소줄에 name(key)에 해당하는 value를 spring!!으로 주면 다음과 같은 화면이 나타남


'SpringMVC > Spring Boot' 카테고리의 다른 글
IntelliJ Gradle 리프레시 | Gradle 안 보일 때 (0) | 2023.09.27 |
---|---|
자주 사용하는 인텔리제이 단축키(window기준) (0) | 2023.09.19 |
@ResponseBody (0) | 2022.09.23 |
동작 원리 (0) | 2022.09.23 |
스프링 프로젝트 생성(Spring Boot) (0) | 2022.09.14 |