<๊ฐ์>
MVC ํจํด์ 3๊ฐ์ง ๊ฒฝ์ฐ๊ฐ ์์.
Controller Method ์ ๋ฐํ ํ์ ์ ๋ฐ๋ผ ๋๋จ
(1)String ๋ฐํํ
//๋
์์ผ์ ์
๋ ฅํ๋ฉด ์์ผ์ ์๋ ค์ฃผ๋ ํ๋ก๊ทธ๋จ
@Controller
public class YoilTellerMVC {
// http://localhost/ch2/getYoilMVC?year=2021&month=10&day=21
@RequestMapping("/getYoilMVC")
//String ๋ฐํ ๋ฉ์๋ -> ๋ฐํ์ ์ฌ์ฉํ view๋ฅผ ์ง์
public String main(int year, int month, int day, Model model) throws IOException {
// ์ ํจ์ฑ ๊ฒ์ฌ, ๋
์ ์ผ์ด ๋ฐ๋ฅด๊ฒ ์
๋ ฅ๋์ง ์์๋ค๋ฉด, ์๋ฌ๋ฅผ ํ์ํ๋ view๋ฅผ ์ด์ฉํด๋ผ
if(!isVaild(year,month,day)) {
return "YoilError";
}
// ์์ผ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ char ๋ณ์์ ๋ฃ์
char yoil = getYoil(year,month,day);
//์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ model์ ์ ์ฅ. ์ด๋ฅผ ํ์ง ์์ผ๋ฉด view์ ๊ฐ์ด ์ ๋ฌ๋์ง ์๊ฒ ๋จ.
model.addAttribute("year",year);
model.addAttribute("month", month);
model.addAttribute("day", day);
model.addAttribute("yoil", yoil);
return "Yoil";
}
private boolean isVaild(int year, int month, int day) {
// TODO Auto-generated method stub
return false;
}
//์์ผ ๊ณ์ฐ
private char getYoil(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return "์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
(2) void ๋ฐํํ
// void ๋ฐํํ์ ์ฐ๊ฒ๋๋ฉด, ์ด์ฉํ view๋ฅผ ์ง์ ํ์ง ๋ชปํ๋ฏ๋ก,
// URL ๋ ๋จ์ด์ ์ด๋ฆ์ด ๊ฐ์ view๋ก ์๋์ผ๋ก ๋์ด๊ฐ๊ฒ ๋๋ค.
public class YoilTellerMVC {
// http://localhost/ch2/getYoilMVC?year=2021&month=10&day=21
@RequestMapping("/getYoilMVC")
public void main(int year, int month, int day, Model model) throws IOException {
// if(!isVaild(year,month,day)) {
// return "YoilError";
// }
char yoil = getYoil(year,month,day);
model.addAttribute("year",year);
model.addAttribute("month", month);
model.addAttribute("day", day);
model.addAttribute("yoil", yoil);
// return "Yoil";
}
private boolean isVaild(int year, int month, int day) {
// TODO Auto-generated method stub
return false;
}
//์์ผ ๊ณ์ฐ
private char getYoil(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return "์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
(3) ModelAndView ๋ฐํํ
package com.fastcampus.ch2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
//๋
์์ผ์ ์
๋ ฅํ๋ฉด ์์ผ์ ์๋ ค์ฃผ๋ ํ๋ก๊ทธ๋จ
@Controller
public class YoilTellerMVC {
// http://localhost/ch2/getYoilMVC?year=2021&month=10&day=21
@RequestMapping("/getYoilMVC")
public ModelAndView main(int year, int month, int day ) throws IOException {
//๋ชจ๋ธ์ ๋ทฐ ๊ฐ์ฒด ์์ฑ
ModelAndView mv = new ModelAndView();
// 2. ์์ผ ๊ณ์ฐ
char yoil = getYoil(year, month, day);
// 3. ๊ณ์ฐํ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ธ์ ์ ์ฅ
mv.addObject("year", year);
mv.addObject("month", month);
mv.addObject("day", day);
mv.addObject("yoil", yoil);
//4. ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค view๋ฅผ ์ง์
mv.setViewName("yoil");
return mv;
}
//์์ผ ๊ณ์ฐ
private char getYoil(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return "์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
(4) ์ค์ค๋ก ํด๋ณด๊ธฐ
//model&view
@Controller
public class YoilTellerMVC {
// http://localhost/ch2/getYoilMVC?year=2021&month=10&day=21
@RequestMapping("/getYoilMVC")
public ModelAndView main(int year, int month, int day) throws IOException {
ModelAndView mv = new ModelAndView();
char yoil = getYoil(year, month,day);
mv.addObject("year", year);
mv.addObject("month", month);
mv.addObject("day", day);
mv.addObject("yoil", yoil);
mv.setViewName("Yoil");
return mv;
}
private boolean isVaild(int year, int month, int day) {
// TODO Auto-generated method stub
return true;
}
//์์ผ ๊ณ์ฐ
private char getYoil(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
return "์ผ์ํ์๋ชฉ๊ธํ ".charAt(dayOfWeek);
}
}
** return์ view ์ด๋ฆ๋ง ์ ์ด์ค๋ ๋๋ ์ด์
: servlet-context.xml์์ prefix์ postfix๋ฅผ ์ค์ ํด์ ํด๋น jsp ํ์ผ๋ก ๊ฐ๋ ๊ฒฝ๋ก์ ๋ค ํ์ฅ์์ธ .jsp๋ฅผ ๋ชจ๋ ์๋์ผ๋ก ์ ์ด์ฃผ๋๋ก ๋์ด์์ด์ ๋ฐ๋ก ์ ์ ํ์๊ฐ ์๋ค
0