tobby48 1 year ago
parent
commit
1521ccf999

+ 30
- 0
src/main/java/kr/co/swh/lecture/opensource/sparkjava/sample/FreeMarkerExample.java View File

@@ -0,0 +1,30 @@
1
+package kr.co.swh.lecture.opensource.sparkjava.sample;
2
+
3
+import static spark.Spark.get;
4
+import static spark.Spark.post;
5
+import static spark.Spark.modelAndView;
6
+
7
+import java.io.IOException;
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+
11
+public class FreeMarkerExample {
12
+
13
+    public static void main(String args[]) throws IOException {
14
+
15
+    	post("/submit", (request, response) -> {
16
+            String author = request.queryParams("myName");
17
+            System.out.println(author);
18
+            return author;
19
+        });
20
+    	
21
+        get("/", (request, response) -> {
22
+            Map<String, Object> attributes = new HashMap<>();
23
+            attributes.put("message", "Hello FreeMarker World");
24
+
25
+            // The hello.ftl file is located in directory:
26
+            // src/test/resources/spark/examples/templateview/freemarker
27
+            return modelAndView(attributes, "search.ftl");
28
+        }, new FreeMarkerTemplateEngine());
29
+    }
30
+}

+ 41
- 0
src/main/java/kr/co/swh/lecture/opensource/sparkjava/sample/FreeMarkerTemplateEngine.java View File

@@ -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
+}

+ 9
- 0
src/main/resources/freemarker/search.ftl View File

@@ -0,0 +1,9 @@
1
+<html>
2
+   <body>
3
+      <form action = "http://127.0.0.1:4567/submit" method = "post">
4
+         <p>Enter Name:</p>
5
+         <p><input type = "text" name = "myName" /></p>
6
+         <p><input type = "submit" value = "submit" /></p>
7
+      </form>
8
+   </body>
9
+</html>