model
public class Star {
String name;
int age;
public Star(String name, int age) {
this.name = name;
this.age = age;
}
}
String name;
int age;
public Star(String name, int age) {
this.name = name;
this.age = age;
}
}
괄호(String name, int age)가 있는 생성자로 만들고
Controller
// [Request sample]
// POST http://localhost:8080/hello/request/form/model
// Header
// Content type: application/x-www-form-urlencoded
// Body
// name=Robbie&age=95
@PostMapping("/form/model")
@ResponseBody
public String helloRequestBodyForm(@ModelAttribute Star star) {
return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}
// POST http://localhost:8080/hello/request/form/model
// Header
// Content type: application/x-www-form-urlencoded
// Body
// name=Robbie&age=95
@PostMapping("/form/model")
@ResponseBody
public String helloRequestBodyForm(@ModelAttribute Star star) {
return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}
html
<h2>POST /hello/request/form/model</h2>
<form method="POST" action="/hello/request/form/model">
<div>
이름: <input name="name" type="text">
</div>
<div>
나이: <input name="age" type="text">
</div>
<button>전송</button>
</form>
<br>
<form method="POST" action="/hello/request/form/model">
<div>
이름: <input name="name" type="text">
</div>
<div>
나이: <input name="age" type="text">
</div>
<button>전송</button>
</form>
<br>
html에서 form에 담아 호출하면 null값이 아니라 입력한 값이 나오게 된다.
728x90
'Programming > Web-Spring' 카테고리의 다른 글
[SpringBoot] 포스트맨 실행 시 에러 Error: Exceeded maxRedirects. Probably stuck in a redirect loop (0) | 2023.06.27 |
---|---|
[SpringBoot] html페이지에서 Json 값을 객체로 받아올때 주의사항 (0) | 2023.06.14 |
[Thymeleaf] SpringBoot에 Thymeleaf 적용하기 (0) | 2023.06.14 |
[SpringBoot] Spring 프로젝트에서 Thymeleaf 기본 설정하는 방법 (0) | 2023.06.14 |
[intellij/spring] h2 (메모리영역에서 사용하는db)설정 방법 (0) | 2022.05.27 |