โ๏ธ ์ ์ฉ ๋์์ Model์ ์์ฑ์ผ๋ก ์๋ ์ถ๊ฐํด์ฃผ๋ ์ ๋ํ ์ด์
@RequestMapping("/getYoilMVC5")
//public String main(@ModelAttribute("myDate") MyDate date, Model m) { // ์๋์ ๋์ผ
public String main(@ModelAttribute MyDate date, Model m) {
char yoil = getYoil(data);
// ์ด๋ฏธ Model์ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ ์ด ์ฒ๋ฆฌ๋ ์ํด๋ ๋๋ค.
//m.addAttribute("myDate", date);
//m.addAttribute("yoil", yoil);
return "yoil";
}
key๋ฅผ ์๋ตํ๋ฉด ์๋์ผ๋ก ์์ ๊ธ์๋ฅผ ์๋ฌธ์๋ก ๋ณํํด์ key๋ก ์ง์ ํด ์ค๋ค.
: ex) ("myDate", MyDate)
์ฐธ์กฐํ์ ์๋ต ๊ฐ๋ฅ โก๏ธ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ถ์ด์์
โ๏ธ @RequestParam: ๊ธฐ๋ณธํ, String์ ์๋ต ๊ฐ๋ฅํ๋ค.
- ์ฐธ์กฐํ ๋งค๊ฐ๋ณ์์ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ โ
: ์ฌ๋ฌ ๊ฐ์ด ๋ฐ์ธ๋ฉ๋๊ธฐ ๋๋ฌธ.- ๊ธฐ๋ณธํ์ด๋ String ๊ฐ์ ๊ฒฝ์ฐ ๊ตณ์ด ๋ชจ๋ธ์ ์ ์ฅํ ํ์ โ
: View์์${param.ํ๋ผ๋ฏธํฐ์ด๋ฆ}
์ผ๋ก ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ.
private @ModelAttribute("yoil") char getYoil(MyDate date) { // ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋๊ณ ์ด ๊ฒฐ๊ณผ๊ฐ model์ ์ ์ฅ๋๋ค.
return getYoil(date.getYear(), date.getMonth(), date.getDay());
}
yoil
์ด๊ณ value๋ ์
๊ฐ ๋๋ค.โ๏ธ YoilTellerMVC5.java
package kr.ac.jipark09.ch2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
// year, month, day๋ฅผ ํ๋๋ก ํฉ์ณ๋ด => ๊ฐ์ฒด๋ก ๋ง๋ค๊ธฐ(dto)
@Controller
public class YoilTellerMVC5 {
@ExceptionHandler(Exception.class)
public String catcher(Exception e) {
e.printStackTrace();
return "yoilError";
}
@RequestMapping("/getYoilMVC5")
public String main(@ModelAttribute("myDate") MyDate date, Model model) throws IOException {
if(!isVaild(date)) {
return "yoilError";
}
char yoil = getYoil(date);
return "yoil";
}
private boolean isVaild(MyDate date) {
return isVaild(date.getYear(), date.getMonth(), date.getDay());
}
private boolean isVaild(int year, int month, int day) {
if(year == 1 || month == 1 || day == 1) {
return false;
}
return (1 <= month && month <= 12) && (1 <= day && day <= 31);
}
private @ModelAttribute("yoil") char getYoil(MyDate date) {
int year = date.getYear();
int month = date.getMonth();
int day = date.getDay();
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return " ์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
โ๏ธ ์์ฒญ ๋งค๊ฐ๋ณ์(form ๋๋ query ๋ฐ์ดํฐ)๋ฅผ Model ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉํ๋ค.
์ฌ์ฉ์๊ฐ ๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด์ ์์ฒญ๋ฐ์ ๊ฐ์ด ์ค์ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋ ๋, ์ค๊ฐ์ญํ ์ ํด ์ฃผ๋ ๊ฒ์ด DataBinder
์ด๋ค.
ํฌ๊ฒ ํ์
๋ณํ๊ณผ ๋ฐ์ดํฐ ๊ฒ์ฆ์ ๊ฑฐ์น๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ BindingResult
์๋ค๊ฐ ์ ์ฅํ๋ค.
๊ฒฐ๊ณผ๊ฐ์ Controller
์ ๋๊ฒจ์ค์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์๋ค.
โ๏ธ ์๋ฌ๊ฐ ๋ฐ์ํด๋ result๋ก Controller์์๋ ๊ฒฐ๊ณผ ๋ณด๊ธฐ ๊ฐ๋ฅ
package kr.ac.jipark09.ch2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class YoilTellerMVC6 {
@ExceptionHandler(Exception.class)
public String catcher(Exception e, BindingResult result) {
System.out.println("result= " + result);
// BindingResult๊ฐ FieldError๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๊ณ ์ด ๊ฐ์ฒด๋ก๋ถํฐ ์ด๋ค ๊ฐ๋ค์ ๋ณผ ์ ์๋ ๋ณด๋ ๊ฒ
FieldError error = result.getFieldError();
System.out.println("error.getCode() = " + error.getCode());
System.out.println("error.getField() = " + error.getField());
System.out.println("error.getDefaultMessage() = " + error.getDefaultMessage());
e.printStackTrace();
return "yoilError";
}
@RequestMapping("/getYoilMVC6")
public String main(MyDate date, BindingResult result) {
System.out.println("result= " + result);
if(!isVaild(date)) {
return "yoilError";
}
char yoil = getYoil(date);
return "yoil";
}
private boolean isVaild(MyDate date) {
return isVaild(date.getYear(), date.getMonth(), date.getDay());
}
private boolean isVaild(int year, int month, int day) {
if(year == -1 || month == -1 || day == -1) {
return false;
}
return (1 <= month && month <= 12) && (1 <= day && day <= 31);
}
private @ModelAttribute("yoil") char getYoil(MyDate date) {
int year = date.getYear();
int month = date.getMonth();
int day = date.getDay();
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return " ์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
result= org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'myDate' on field 'day': rejected value [aa]; codes [typeMismatch.myDate.day,typeMismatch.day,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [myDate.day,day]; arguments []; default message [day]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'day'; nested exception is java.lang.NumberFormatException: For input string: "aa"]
error.getCode() = typeMismatch
error.getField() = day
error.getDefaultMessage() = Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'day'; nested exception is java.lang.NumberFormatException: For input string: "aa"
BindingResult๋ ์๋ ํผ์ ๋ ฅํ ๋ ์ฐ๋๊ฑด๋ฐ, ์ฌ๊ธฐ์๋ ์ฃผ์๋ฅผ ์ง์ ์ ๋ ฅํ๊ณ GET์ผ๋ก ๋ค์ด์ค๋ ๊ฑฐ๋ผ ์คํ ๋์ง ์๋๋ค.
๋ณดํต์ Controller ๋ฉ์๋๊ฐ ๋ฐ์ ์ ์๋๋ฐ ์ง๊ธ์ ๋ธ๋ผ์ฐ์ ์์ ์ง์ url์ ์น๊ณ ํ๋ฒ์ ๋ค์ด์๊ธฐ ๋๋ฌธ์ Controller๊น์ง ๋ชป๊ฐ๊ณ ๊ทธ ์์์ ์๋ฌ๊ฐ ๋ฐ์ํด Handler๋ก ์ฒ๋ฆฌ๋ฅผ ํ ๊ฒ. ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ๋ ์๋.
์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ๋ form์ผ๋ก ๋ฐ๋ ๊ฒฝ์ฐ๊ณ . ๊ทธ๋ด ๋๋ Controller์ BindingResult๋ก ๊ฐ์ด ๋ค ๋์ด์ฌ ์ ์๋ค.
Reference
:https://fastcampus.co.kr/dev_academy_nks