에러 모음

MissingServletRequestParameterException

junani0v0 2024. 4. 8. 14:25

< 에러 메시지 >

.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present

 

< 원인 >

@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name,Model model ) {
    //외부에서 파라미터 받고 model에 담음
model.addAttribute("name",name);    //파라미터로 넘어온 name을 model에 담아 넘겨줌
return "hello-template";
}

@RequestParam("name")에서 name값을 안줘서 발생

 

< 해결 >

@GetMapping임으로 주소창에 hello-mvc?name=spring처럼 ?뒤에 값을 작성해준다

그러면 name은 spring으로 바뀌고 model에 담기고 hello-template.html로 넘어가서 ${name}에 model의 값인 spring으로 치환됨

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 

'에러 모음' 카테고리의 다른 글

o.s.b.d.LoggingFailureAnalysisReporter  (0) 2024.04.09
AssertionFailedError  (0) 2024.04.08
does not override abstract  (0) 2024.04.05
ClassCastException  (0) 2024.04.02
method does not override  (0) 2024.04.01