|
@@ -0,0 +1,41 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.sparkjava.sample;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.io.StringWriter;
|
|
5
|
+
|
|
6
|
+import freemarker.template.Configuration;
|
|
7
|
+import freemarker.template.Template;
|
|
8
|
+import freemarker.template.TemplateException;
|
|
9
|
+import spark.ModelAndView;
|
|
10
|
+import spark.TemplateEngine;
|
|
11
|
+
|
|
12
|
+public class FreeMarkerTemplateEngine extends TemplateEngine {
|
|
13
|
+
|
|
14
|
+ private Configuration configuration;
|
|
15
|
+
|
|
16
|
+ public FreeMarkerTemplateEngine() {
|
|
17
|
+ this.configuration = createFreemarkerConfiguration();
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ @Override
|
|
21
|
+ public String render(ModelAndView modelAndView) {
|
|
22
|
+ try {
|
|
23
|
+ StringWriter stringWriter = new StringWriter();
|
|
24
|
+
|
|
25
|
+ Template template = configuration.getTemplate(modelAndView.getViewName());
|
|
26
|
+ template.process(modelAndView.getModel(), stringWriter);
|
|
27
|
+
|
|
28
|
+ return stringWriter.toString();
|
|
29
|
+ } catch (IOException e) {
|
|
30
|
+ throw new IllegalArgumentException(e);
|
|
31
|
+ } catch (TemplateException e) {
|
|
32
|
+ throw new IllegalArgumentException(e);
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ private Configuration createFreemarkerConfiguration() {
|
|
37
|
+ Configuration retVal = new Configuration();
|
|
38
|
+ retVal.setClassForTemplateLoading(FreeMarkerTemplateEngine.class, "/freemarker/"); // 수정할 것.
|
|
39
|
+ return retVal;
|
|
40
|
+ }
|
|
41
|
+}
|