| 73 | } |
| 74 | |
| 75 | private static void render(Model model, String viewName) throws IOException { |
| 76 | String result = ""; |
| 77 | |
| 78 | // 1. 뷰의 내용을 한줄씩 읽어서 하나의 문자열로 만든다. |
| 79 | Scanner sc = new Scanner(new File("src/main/webapp/WEB-INF/views/"+viewName+".jsp"), "utf-8"); |
| 80 | |
| 81 | while(sc.hasNextLine()) |
| 82 | result += sc.nextLine()+ System.lineSeparator(); |
| 83 | |
| 84 | // 2. model을 map으로 변환 |
| 85 | Map map = model.asMap(); |
| 86 | |
| 87 | // 3.key를 하나씩 읽어서 template의 ${key}를 value바꾼다. |
| 88 | Iterator it = map.keySet().iterator(); |
| 89 | |
| 90 | while(it.hasNext()) { |
| 91 | String key = (String)it.next(); |
| 92 | |
| 93 | // 4. replace()로 key를 value 치환한다. |
| 94 | result = result.replace("${"+key+"}", ""+map.get(key)); |
| 95 | } |
| 96 | |
| 97 | // 5.렌더링 결과를 출력한다. |
| 98 | System.out.println(result); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* [실행결과] |