MCPcopy Create free account
hub / github.com/castello/spring_basic / MethodCall2

Class MethodCall2

ch2/MethodCall2.java:13–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11import org.springframework.validation.support.BindingAwareModelMap;
12
13public class MethodCall2 {
14 public static void main(String[] args) throws Exception{
15
16 Class clazz = Class.forName("com.fastcampus.ch2.YoilTellerMVC");
17 Object obj = clazz.newInstance();
18
19 Method main = clazz.getDeclaredMethod("main", int.class, int.class, int.class, Model.class);
20
21 Model model = new BindingAwareModelMap();
22 System.out.println("[before] model="+model);
23
24 // String viewName = obj.main(2021, 10, 1, model);
25 String viewName = (String)main.invoke(obj, new Object[] { 2021, 10, 1, model });
26 System.out.println("viewName="+viewName);
27
28 // Model의 내용을 출력
29 System.out.println("[after] model="+model);
30
31 // 텍스트 파일을 이용한 rendering
32 render(model, viewName);
33 } // main
34
35 static void render(Model model, String viewName) throws IOException {
36 String result = "";
37
38 // 1. 뷰의 내용을 한줄씩 읽어서 하나의 문자열로 만든다.
39 Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8");
40
41 while(sc.hasNextLine())
42 result += sc.nextLine()+ System.lineSeparator();
43
44 // 2. model을 map으로 변환
45 Map map = model.asMap();
46
47 // 3.key를 하나씩 읽어서 template의 ${key}를 value바꾼다.
48 Iterator it = map.keySet().iterator();
49
50 while(it.hasNext()) {
51 String key = (String)it.next();
52
53 // 4. replace()로 key를 value 치환한다.
54 result = result.replace("${"+key+"}", ""+map.get(key));
55 }
56
57 // 5.렌더링 결과를 출력한다.
58 System.out.println(result);
59 }
60}
61
62/* [실행결과]
63[before] model={}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected