freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [01/16] incubator-freemarker git commit: FREEMARKER-55: Adding skeletal FreemarkerView
Date Tue, 04 Jul 2017 07:44:12 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 19071828a -> b230056e7


FREEMARKER-55: Adding skeletal FreemarkerView


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/584b46b8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/584b46b8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/584b46b8

Branch: refs/heads/3
Commit: 584b46b86aee16134f1f1c9e6d2cdb79bb7f8016
Parents: f3b89b5
Author: Woonsan Ko <woonsan@apache.org>
Authored: Wed Jun 28 16:18:14 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Wed Jun 28 16:18:14 2017 -0400

----------------------------------------------------------------------
 freemarker-spring/build.gradle                  |  3 +-
 .../spring/web/view/AbstractFreemarkerView.java | 66 +++++++++++++++++
 .../spring/web/view/FreemarkerView.java         | 23 ++++++
 .../spring/web/view/FreemarkerViewTest.java     | 78 ++++++++++++++++++++
 4 files changed, 169 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/584b46b8/freemarker-spring/build.gradle
----------------------------------------------------------------------
diff --git a/freemarker-spring/build.gradle b/freemarker-spring/build.gradle
index 3efdbe5..60e03b2 100644
--- a/freemarker-spring/build.gradle
+++ b/freemarker-spring/build.gradle
@@ -27,7 +27,8 @@ inAggregateJavadoc = true
 dependencies {
     compile project(":freemarker-core")
 
-    compileOnly "javax.servlet:servlet-api:2.5"
+    // TODO: what's difference between compileOnly and compile??
+    compile "org.apache.geronimo.specs:geronimo-servlet_3.0_spec:1.0"
 
     def springVersion = "4.3.9.RELEASE"
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/584b46b8/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
new file mode 100644
index 0000000..a2a2e8b
--- /dev/null
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.freemarker.spring.web.view;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.ParseException;
+import org.apache.freemarker.core.Template;
+import org.apache.freemarker.core.TemplateNotFoundException;
+import org.apache.freemarker.core.templateresolver.MalformedTemplateNameException;
+import org.springframework.web.servlet.view.AbstractView;
+
+public abstract class AbstractFreemarkerView extends AbstractView {
+
+    private Configuration configuration;
+    private String templateName;
+
+    public Configuration getConfiguration() {
+        return configuration;
+    }
+
+    public void setConfiguration(Configuration configuration) {
+        this.configuration = configuration;
+    }
+
+    public String getTemplateName() {
+        return templateName;
+    }
+
+    public void setTemplateName(String templateName) {
+        this.templateName = templateName;
+    }
+
+    @Override
+    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest
request,
+            HttpServletResponse response) throws Exception {
+        getTemplate().process(model, response.getWriter());
+    }
+
+    protected Template getTemplate()
+            throws TemplateNotFoundException, MalformedTemplateNameException, ParseException,
IOException {
+        return getConfiguration().getTemplate(getTemplateName());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/584b46b8/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
new file mode 100644
index 0000000..da3ad09
--- /dev/null
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.freemarker.spring.web.view;
+
+public class FreemarkerView extends AbstractFreemarkerView {
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/584b46b8/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
new file mode 100644
index 0000000..64f50e0
--- /dev/null
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.freemarker.spring.web.view;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.templateresolver.impl.StringTemplateLoader;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockServletContext;
+
+public class FreemarkerViewTest {
+
+    private ServletContext servletContext;
+
+    private Configuration configuration;
+
+    @Before
+    public void setUp() throws Exception {
+        servletContext = new MockServletContext();
+
+        StringTemplateLoader templateLoader = new StringTemplateLoader();
+        templateLoader.putTemplate("hello.ftl", "Hello, ${name!\"World\"}! Visit count: ${visitCount!0}");
+        configuration = new Configuration.Builder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS)
+                .templateLoader(templateLoader).build();
+    }
+
+    @Test
+    public void testViewRendering() throws Exception {
+        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
+        request.setContextPath("/mycontext");
+        request.setServletPath("/myservlet");
+        request.setPathInfo(";mypathinfo");
+        request.setQueryString("?param1=value1");
+
+        FreemarkerView view = new FreemarkerView();
+        view.setServletContext(servletContext);
+        view.setConfiguration(configuration);
+        view.setTemplateName("hello.ftl");
+
+        int visitCount = 0;
+        Map<String, Object> model = new HashMap<String, Object>();
+
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        view.render(model, request, response);
+        assertEquals("Hello, World! Visit count: 0", response.getContentAsString());
+
+        response = new MockHttpServletResponse();
+        model.put("name", "Dan");
+        model.put("visitCount", ++visitCount);
+        view.render(model, request, response);
+        assertEquals("Hello, Dan! Visit count: 1", response.getContentAsString());
+    }
+}


Mime
View raw message