[ helloController ]
@controller
public class HelloController {
@Getmapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAtrtribute("name","name");
return "hello-template";
}
}
@RequestParam("name") String name
@RequestParam("가져올 데이터 이름") [데이터 타입] [가져온 데이터를 담을 변수명]
예시
@RequestParam(name="원하는 파라미터 명")
을 이용하면, 파라미터명을 바꿀 수 있다.@RequestParam String id
: http://localhost:8080/hello-mvc.html?id=안녕@RequestParam(name = "password") String pwd
: http://localhost:8080/hello-mvc.html?password=블라블라
return "hello-template";
: 리턴하는 이름과 같은 html 을 찾아 이동
[ hello-template.html ]
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text=" 'hello' + ${name}"></p>
</body>
</html>
에러 발생
Required String parameter 'name' is not present 네임이 없다??
해결 (방법 2가지)
방법 1.
@RequestParam(value = "name", required=false)
required=false 처럼 required 속성을 넣어 value 값에 아무것도 넣지않고 파라미터를 호출가능하도록 한다.
방법 2.
required default 값이 true 이다.
required=true로 한다면 url에 직접 값을 넣어준다.
: http://localhost:8080/hello-mvc.html?name=spring
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAtrtribute("name","name");
return "hello-template";
}
컨트롤러에서 name은 spring 으로 들어가고
model 에서도 name = "spring" 이 담긴다.
hello-template.html 로 리턴하여 ${name}에 spring 이 들어가게 된다.
반응형
LIST
'에러' 카테고리의 다른 글
이클립스 incompatible jvm 에러해결 방법 (0) | 2023.10.25 |
---|---|
h2 라이브러리 사용 + 이클립스 실행시 에러 (0) | 2023.09.22 |
이클립스 프로젝트 생성할 때 발생한 에러 해결 (0) | 2023.07.24 |
h2 database 연결 실패 에러 not found, File corrupted while reading record 해결방법 (0) | 2023.07.22 |
Multiple markers at this lin 에러 | ajax 기능이 안될 때 해결법 (0) | 2022.11.07 |