| 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 | /* [실행결과] |