[Spring] @ModelAttribute

Jeiniยท2023๋…„ 5์›” 20์ผ
0

๐Ÿƒย  Spring

๋ชฉ๋ก ๋ณด๊ธฐ
5/33
post-thumbnail

๐Ÿ’ก @ModelAttribute


โœ”๏ธ ์ ์šฉ ๋Œ€์ƒ์„ Model์˜ ์†์„ฑ์œผ๋กœ ์ž๋™ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ์• ๋„ˆํ…Œ์ด์…˜

โœ๏ธ MyDate 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());
}
  • key๋Š” 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);
	}

}

๐Ÿ’ก WebDataBinder


โœ”๏ธ ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜(form ๋˜๋Š” query ๋ฐ์ดํ„ฐ)๋ฅผ Model ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด์„œ ์š”์ฒญ๋ฐ›์€ ๊ฐ’์ด ์‹ค์ œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋  ๋•Œ, ์ค‘๊ฐ„์—ญํ• ์„ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด DataBinder ์ด๋‹ค.

  • ํฌ๊ฒŒ ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ์„ ๊ฑฐ์น˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ BindingResult์—๋‹ค๊ฐ€ ์ €์žฅํ•œ๋‹ค.

  • ๊ฒฐ๊ณผ๊ฐ’์„ Controller ์— ๋„˜๊ฒจ์ค˜์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธ BindingResult ์˜ˆ์ œ

โœ”๏ธ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด๋„ 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

profile
Fill in my own colorful colors๐ŸŽจ

0๊ฐœ์˜ ๋Œ“๊ธ€