[ 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

+ Recent posts