1. methodcall2
package com.fastcampus.ch2;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
public class MethodCall2 {
public static void main(String[] args) throws Exception{
// 1. YoilTellerMVC์ ๊ฐ์ฒด๋ฅผ ์์ฑ
// (1) ํด๋์ค ๊ฐ์ฒด ์์ฑ => YoilTellerMVC์ ์ค๊ณ๋ ๋ฒ ๋
Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
// (2) ๋ฒ ๋ ์ค๊ณ๋๋ฅผ ์ด์ฉํด YoiltellerMVC ๊ฐ์ฒด ์์ฑ
Object obj = clazz.newInstance();
// 2. ์์ผํ
๋ฌ์ main๋ฉ์๋์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค. ๋งค์๋ ์ด๋ฆ, ๋งค์๋์ ๋งค๊ฐ๋ณ์ 4๊ฐ // ์ด๋ฆ์ด main์ธ ๊ฒ์ด ๋ค์ ์์ ์ ์์ผ๋ฏ๋ก ๊ผญ ๋ค์ ๋งค๊ฐ ๋ณ์๊น์ง ์ ์ด์ ๋ด๊ฐ ์ฐ๊ณ ์ถ์ ๋
์์ ํน์ ํด์ผํจ.
Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
// 3. ๋ชจ๋ธ์ ์์ฑ
Model model = new BindingAwareModelMap();
System.out.println("[before] model="+model);
// 4. main ๋งค์๋๋ฅผ ํธ์ถ
// ์ฐ๋ฆฌ๊ฐ ํธ์ถํ ๋งค์๋์ ๋งค๊ฐ ๋ณ์๊ฐ ๋ค ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋ฐ์ค์ฒ๋ผ ํธ์ถ ๋ชปํ๊ณ Reflection API๋ก ํธ์ถํ๋ ๊ฒ ํจ์ฌ ์ ์ฐํจ.
// String viewName = obj.main(2021, 10, 1, model); // ์๋ ์ค๊ณผ ๋์ผ
//Reflection API๋ฅผ ์ด์ฉํ ํธ์ถ (ํ๋ ์ฝ๋ฉ)
// ๋งค์๋.invoke()๋ ํด๋น ๋งค์๋๋ฅผ ์คํํ๋ ๋งค์๋
// ํํ: ๋งค์๋A.invoke(A๋ฅผ ์คํํ ๊ฐ์ฒด, A์ ํ๋ผ๋ฏธํฐ ๊ฐ)
// ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ฐฐ์ด๋ก ๋ค์ด๊ฐ.
// ๊ฐ์ฒด๊ฐ ํ์
์ด object์ด๋ฏ๋ก ํ ๋ณํ ํ์
String viewName = (String)main.invoke(obj, new Object[] { 2021, 10, 1, model });
System.out.println("viewName="+viewName);
// Model์ ๋ด์ฉ์ ์ถ๋ ฅ
System.out.println("[after] model="+model);
// ํ
์คํธ ํ์ผ์ ์ด์ฉํ rendering
render(model, viewName);
} // main
static void render(Model model, String viewName) throws IOException {
String result = "";
// 1. ๋ทฐ์ ๋ด์ฉ์ ํ์ค์ฉ ์ฝ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8");
while(sc.hasNextLine())
result += sc.nextLine()+ System.lineSeparator();
// 2. model์ map์ผ๋ก ๋ณํ
Map map = model.asMap();
// 3.key๋ฅผ ํ๋์ฉ ์ฝ์ด์ template์ ${key}๋ฅผ value๋ฐ๊พผ๋ค.
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
// 4. replace()๋ก key๋ฅผ value ์นํํ๋ค.
result = result.replace("${"+key+"}", ""+map.get(key));
}
// 5.๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
System.out.println(result);
}
}
2.Methodcall3
์์์๋ ๋ฉ์ธ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ฅผ ํ๋์ฝ๋ฉ์ ํตํด ๋ฃ์๋ค๋ฉด, ์ฌ๊ธฐ์๋ ๋์ ์ผ๋ก (์๋ณธ์์ ๋งค๊ฐ ๋ณ์ ๊ฐ์ด ๋ณํ ๋๋ง๋ค, class๋ก ๋ง๋ ์ฌ๋ณธ์์๋ ๋งค๊ฐ๋ณ์์ ๊ฐ์ด๊ฐ ๋ฐ๋ ์ ์๊ฒ) ๋ง๋ ๋ค.
package com.fastcampus.ch2;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
public class MethodCall3 {
public static void main(String[] args) throws Exception{
// 1. ์์ฒญํ ๋ ์ ๊ณต๋ ๊ฐ - request.getParameterMap(); -> ์คํธ๋ง ์ฟผ๋ฆฌ์ Name๊ณผ Value๋ฅผ Map ํํ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
Map map = new HashMap();
map.put("year", "2021");
map.put("month", "10");
map.put("day", "1");
Model model = null;
Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
Object obj = clazz.newInstance();
// YoilTellerMVC.main(int year, int month, int day, Model model)
Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
Parameter[] paramArr = main.getParameters(); //๋ฉ์ธ ๋งค์๋์ ๋งค๊ฐ ๋ณ์ ๋ชฉ๋ก์ ๊ฐ์ ธ์จ๋ค.
Object[] argArr = new Object[main.getParameterCount()]; // ๋งค๊ฐ๋ณ์์ ๊ฐฏ์์ ๊ฐ์ ๊ธธ์ด์ ์ค๋ธ์ ํธ ๋ฐฐ์ด์ ์์ฑ
for(int i=0;i<paramArr.length;i++) {
String paramName = paramArr[i].getName();
Class paramType = paramArr[i].getType();
Object value = map.get(paramName); // map์์ ๋ชป์ฐพ์ผ๋ฉด value๋ null
// paramType์ค์ Model์ด ์์ผ๋ฉด, ์์ฑ & ์ ์ฅ
if(paramType==Model.class) {
// ํด๋น ๋ฐฐ์ด์๋ ๋ชจ๋ธ์ ์์ฑ
argArr[i] = model = new BindingAwareModelMap();
} else if(value != null) { // map์ paramName์ด ์์ผ๋ฉด,
// value์ parameter์ ํ์
์ ๋น๊ตํด์, ๋ค๋ฅด๋ฉด ๋ณํํด์ ์ ์ฅ
argArr[i] = convertTo(value, paramType);
}
}
System.out.println("paramArr="+Arrays.toString(paramArr));
System.out.println("argArr="+Arrays.toString(argArr));
// Controller์ main()์ ํธ์ถ - YoilTellerMVC.main(int year, int month, int day, Model model)
String viewName = (String)main.invoke(obj, argArr);
System.out.println("viewName="+viewName);
// Model์ ๋ด์ฉ์ ์ถ๋ ฅ
System.out.println("[after] model="+model);
// ํ
์คํธ ํ์ผ์ ์ด์ฉํ rendering
render(model, viewName);
} // main
private static Object convertTo(Object value, Class type) { // Class type์ ๋งค๊ฐ๋ณ์์ type์ ์๋ฏธ
if(type==null || value==null || type.isInstance(value)) // ํ์
์ด ๊ฐ์ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
return value;
// ํ์
์ด ๋ค๋ฅด๋ฉด, ๋ณํํด์ ๋ฐํ
if(String.class.isInstance(value) && type==int.class) { // String -> int
return Integer.valueOf((String)value);
} else if(String.class.isInstance(value) && type==double.class) { // String -> double
return Double.valueOf((String)value);
}
return value;
}
private static void render(Model model, String viewName) throws IOException {
String result = "";
// 1. ๋ทฐ์ ๋ด์ฉ์ ํ์ค์ฉ ์ฝ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8");
while(sc.hasNextLine())
result += sc.nextLine()+ System.lineSeparator();
// 2. model์ map์ผ๋ก ๋ณํ
Map map = model.asMap();
// 3.key๋ฅผ ํ๋์ฉ ์ฝ์ด์ template์ ${key}๋ฅผ value๋ฐ๊พผ๋ค.
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
// 4. replace()๋ก key๋ฅผ value ์นํํ๋ค.
result = result.replace("${"+key+"}", ""+map.get(key));
}
// 5.๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
System.out.println(result);
}
}
3.DispatcherServlet
package com.fastcampus.ch2;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
//Spring์ @Controller + @RequestMapping ํฉ์น ๊ฒ
// ์๋ธ๋ฆฟ์ ๋งค์๋ ๋จ์๋ก ๋งคํ์ด ์๋๊ณ ํด๋์ค ๋จ์๋ก ๋งคํ์ด ๋จ.
// ํด๋น ์ด๋
ธํ
์ด์
์ ๊ฐ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ด์ ์๋ฌ๊ฐ ๋จ. buildpath -> configure buildpath -> runtime ์ถ๊ฐํ๋ฉด ํฐ์บฃ์์ ํด๋น ์ด๋
ธํ
์ด์
๊ฐ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ถ๊ฐ๋จ.
//@WebServlet์ HttpServlet ์์, ๋งค์๋ ์ด๋ฆ์ด service์ธ ๊ฒ, ๋งค๊ฐ๋ณ์๋ก request์ reponse ๋ฐ๋ ๊ฒ์ด ํญ์ ๊ณ ์ ๋ ํ์
@WebServlet("/myDispatcherServlet") // http://localhost/ch2/myDispatcherServlet?year=2021&month=10&day=1
public class MyDispatcherServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map map = request.getParameterMap();
Model model = null;
String viewName = "";
try {
Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
Object obj = clazz.newInstance();
// 1. main๋ฉ์๋์ ์ ๋ณด๋ฅผ ์ป๋๋ค.
Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
// 2. main๋ฉ์๋์ ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก(paramArr)์ ์ฝ์ด์ ๋ฉ์๋ ํธ์ถ์ ์ฌ์ฉํ ์ธ์ ๋ชฉ๋ก(argArr)์ ๋ง๋ ๋ค.
Parameter[] paramArr = main.getParameters();
Object[] argArr = new Object[main.getParameterCount()];
for(int i=0;i<paramArr.length;i++) {
String paramName = paramArr[i].getName();
Class paramType = paramArr[i].getType();
Object value = map.get(paramName);
// paramType์ค์ Model์ด ์์ผ๋ฉด, ์์ฑ & ์ ์ฅ
if(paramType==Model.class) {
argArr[i] = model = new BindingAwareModelMap();
} else if(paramType==HttpServletRequest.class) {
argArr[i] = request;
} else if(paramType==HttpServletResponse.class) {
argArr[i] = response;
} else if(value != null) { // map์ paramName์ด ์์ผ๋ฉด,
// value์ parameter์ ํ์
์ ๋น๊ตํด์, ๋ค๋ฅด๋ฉด ๋ณํํด์ ์ ์ฅ
String strValue = ((String[])value)[0]; // getParameterMap()์์ ๊บผ๋ธ value๋ String๋ฐฐ์ด์ด๋ฏ๋ก ๋ณํ ํ์
argArr[i] = convertTo(strValue, paramType);
}
}
// 3. Controller์ main()์ ํธ์ถ - YoilTellerMVC.main(int year, int month, int day, Model model)
viewName = (String)main.invoke(obj, argArr);
} catch(Exception e) {
e.printStackTrace();
}
// 4. ํ
์คํธ ํ์ผ์ ์ด์ฉํ rendering
render(model, viewName, response);
} // main
private Object convertTo(Object value, Class type) {
if(type==null || value==null || type.isInstance(value)) // ํ์
์ด ๊ฐ์ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
return value;
// ํ์
์ด ๋ค๋ฅด๋ฉด, ๋ณํํด์ ๋ฐํ
if(String.class.isInstance(value) && type==int.class) { // String -> int
return Integer.valueOf((String)value);
} else if(String.class.isInstance(value) && type==double.class) { // String -> double
return Double.valueOf((String)value);
}
return value;
}
private String getResolvedViewName(String viewName) {
return getServletContext().getRealPath("/WEB-INF/views") +"/"+viewName+".jsp";
}
private void render(Model model, String viewName, HttpServletResponse response) throws IOException {
String result = "";
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
// 1. ๋ทฐ์ ๋ด์ฉ์ ํ์ค์ฉ ์ฝ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
Scanner sc = new Scanner(new File(getResolvedViewName(viewName)), "utf-8");
while(sc.hasNextLine())
result += sc.nextLine()+ System.lineSeparator();
// 2. model์ map์ผ๋ก ๋ณํ
Map map = model.asMap();
// 3.key๋ฅผ ํ๋์ฉ ์ฝ์ด์ template์ ${key}๋ฅผ value๋ฐ๊พผ๋ค.
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
// 4. replace()๋ก key๋ฅผ value ์นํํ๋ค.
result = result.replace("${"+key+"}", map.get(key)+"");
}
// 5.๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
out.println(result);
}
}
4. ์ค์ค๋ก ํด๋ณด๊ธฐ
public class MethodCall2 {
public static void main(String[] args) throws Exception{
Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
Object obj = clazz.newInstance();
Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
Model model = new BindingAwareModelMap();
System.out.println("[before] model="+model);
String viewName = (String)main.invoke(obj, new Object[] {2021, 10,1,model});
System.out.println("viewName="+viewName);
System.out.println("[after]Model=" + model);
render(model,viewName);
} // main
static void render(Model model, String viewName) throws IOException {
String result = "";
Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8");
while(sc.hasNextLine())
result += sc.nextLine() + System.lineSeparator();
// ๋ชจ๋ธ์ ๋ด์ฉ์ key์ value๋ก ๋๋ ์ Map์ผ๋ก ๋ณด์ฌ์ฃผ๋ ํจ์ .asMap();
Map map = model.asMap();
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
result = result.replace("${"+key+"}", ""+map.get(key));
System.out.println(result);
}
}
}
public class MethodCall3 {
public static void main(String[] args) throws Exception{
// 1. ์์ฒญํ ๋ ์ ๊ณต๋ ๊ฐ - request.getParameterMap(); -> ์คํธ๋ง ์ฟผ๋ฆฌ์ Name๊ณผ Value๋ฅผ Map ํํ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
Map map = new HashMap();
map.put("year", "2021");
map.put("month", "10");
map.put("day", "1");
Model model = null;
Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
Object obj = clazz.newInstance();
// YoilTellerMVC.main(int year, int month, int day, Model model)
Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
Parameter[] paramArr = main.getParameters();
Object[] argArr = new Object[main.getParameterCount()];
for(int i=0;i<paramArr.length;i++) {
String paramName = paramArr[i].getName();
Class paramType = paramArr[i].getType();
Object value = map.get(paramName);
if(paramType == Model.class) {
argArr[i] = model = new BindingAwareModelMap();
}else if(value != null) {
argArr[i] = convertTo(value, paramType);
}
}
System.out.println("paramArr=" + Arrays.toString(paramArr));
System.out.println("argArr=" + Arrays.toString(argArr));
// Controller์ main()์ ํธ์ถ - YoilTellerMVC.main(int year, int month, int day, Model model)
String viewName = (String)main.invoke(obj, argArr);
System.out.println("viewName="+viewName);
// Model์ ๋ด์ฉ์ ์ถ๋ ฅ
System.out.println("[after] model="+model);
// ํ
์คํธ ํ์ผ์ ์ด์ฉํ rendering
render(model, viewName);
} // main
private static Object convertTo(Object value, Class type) { // Class type์ ๋งค๊ฐ๋ณ์์ type์ ์๋ฏธ
// type.isInstance(value) type์ด๋ ๊ฐ์ฒด์ ํด๋์ค๊ฐ value์ ๊ฐ๋ค๋ฉด, ๋ ๋ค ์๋ฃํ์ผ ๊ฒฝ์ฐ, ์๋ฃํ์ด ๊ฐ๋ค๋ฉด์ ๋ป
if(type==null || value == null || type.isInstance(value))
return value;
if(String.class.isInstance(value)&& type==int.class) { //param type์ด int ์ธ๋ฐ value์ type์ String์ผ ๊ฒฝ์ฐ paramType์ String์์ int๋ก ๋ฐ๊ฟ๋ผ
return Integer.valueOf((String)value);
} else if(String.class.isInstance(value)&&type==double.class) {
return Double.valueOf((String)value);
}
return value;
}
private static void render(Model model, String viewName) throws IOException {
String result = "";
// 1. ๋ทฐ์ ๋ด์ฉ์ ํ์ค์ฉ ์ฝ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8");
while(sc.hasNextLine())
result += sc.nextLine()+ System.lineSeparator();
// 2. model์ map์ผ๋ก ๋ณํ
Map map = model.asMap();
// 3.key๋ฅผ ํ๋์ฉ ์ฝ์ด์ template์ ${key}๋ฅผ value๋ฐ๊พผ๋ค.
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
// 4. replace()๋ก key๋ฅผ value ์นํํ๋ค.
result = result.replace("${"+key+"}", ""+map.get(key));
}
// 5.๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
System.out.println(result);
}
}
0