스프링부트
[스프링 부트] 동일한 name으로 controller로 값 받아오기
sm_hope
2022. 8. 27. 21:05
만약 동일한 name으로 변수를 보내야 한다고 가정을 해보자.
HTML
<form th:action="@{|/test|}" method="post">
<input type="text" name="subject">
<input type="text" name="subject">
<input type="text" name="subject">
<input type="text" name="subject">
<input type="text" name="subject">
<input type="submit" text="제출">
</form>
subject라는 곳으로 5개의 데이터를 한번에 받을 것이다.
Controller
@PostMapping("/test")
public String isfindList(@RequestParam(name= "subject") List<String> subjectList){
for(String subject: subjectList){
System.out.println(subject);
}
return "test.html";
}
@RequestParam으로 input에 쓴 name을 지정해준다.
그 데이터를 List형태로 받으면 간단히 해결된다.
예시 적용
다음과 같이 입력을 하고 제출을 누르면,
정상적으로 입력값이 출력되는 것을 볼 수 있다.