You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by wo...@apache.org on 2018/01/25 01:54:38 UTC
incubator-freemarker git commit: FREEMARKER-55: Adding form.hidden
directive
Repository: incubator-freemarker
Updated Branches:
refs/heads/3 c38255647 -> 6bfbfd381
FREEMARKER-55: Adding form.hidden directive
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/6bfbfd38
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/6bfbfd38
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/6bfbfd38
Branch: refs/heads/3
Commit: 6bfbfd3817b2003f56cfa0d8533ab10d68c4d952
Parents: c382556
Author: Woonsan Ko <wo...@apache.org>
Authored: Wed Jan 24 20:54:30 2018 -0500
Committer: Woonsan Ko <wo...@apache.org>
Committed: Wed Jan 24 20:54:30 2018 -0500
----------------------------------------------------------------------
.../form/HiddenInputTemplateDirectiveModel.java | 134 +++++++++++++++++++
.../SpringFormTemplateCallableHashModel.java | 1 +
.../form/TextareaTemplateDirectiveModel.java | 2 +-
.../HiddenInputTemplateDirectiveModelTest.java | 71 ++++++++++
...PasswordInputTemplateDirectiveModelTest.java | 6 +-
.../form/hidden-input-directive-usages.ftlh | 63 +++++++++
6 files changed, 273 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModel.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModel.java
new file mode 100644
index 0000000..ba9c192
--- /dev/null
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModel.java
@@ -0,0 +1,134 @@
+/*
+ * 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.model.form;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.freemarker.core.CallPlace;
+import org.apache.freemarker.core.Environment;
+import org.apache.freemarker.core.TemplateException;
+import org.apache.freemarker.core.model.ArgumentArrayLayout;
+import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
+import org.apache.freemarker.core.model.TemplateModel;
+import org.apache.freemarker.core.util.CallableUtils;
+import org.apache.freemarker.core.util.StringToIndexMap;
+import org.springframework.web.servlet.support.RequestContext;
+
+/**
+ * Provides <code>TemplateModel</code> for data-binding-aware HTML '{@code input}' element with a '{@code type}'
+ * of '{@code hidden}'.
+ * <P>
+ * This directive supports the following parameters:
+ * <UL>
+ * <LI><code>path</code>: The first positional parameter pointing to the bean or bean property to bind status information for.</LI>
+ * <LI>
+ * ... TODO ...
+ * </LI>
+ * </UL>
+ * </P>
+ * <P>
+ * Some valid example(s):
+ * </P>
+ * <PRE>
+ * <@form.hidden 'user.email' />
+ * </PRE>
+ * <P>
+ * <EM>Note:</EM> Unlike Spring Framework's <code><form:hidden /></code> JSP Tag Library, this directive
+ * does not support <code>htmlEscape</code> parameter. It always renders HTML's without escaping
+ * because it is much easier to control escaping in FreeMarker Template expressions.
+ * </P>
+ */
+
+class HiddenInputTemplateDirectiveModel extends AbstractHtmlElementTemplateDirectiveModel {
+
+ public static final String NAME = "hidden";
+
+ private static final int NAMED_ARGS_OFFSET = AbstractHtmlElementTemplateDirectiveModel.ARGS_LAYOUT
+ .getPredefinedNamedArgumentsEndIndex();
+
+ private static final int DISABLED_PARAM_IDX = NAMED_ARGS_OFFSET;
+ private static final String DISABLED_PARAM_NAME = "disabled";
+
+ protected static final ArgumentArrayLayout ARGS_LAYOUT =
+ ArgumentArrayLayout.create(
+ 1,
+ false,
+ StringToIndexMap.of(AbstractHtmlElementTemplateDirectiveModel.ARGS_LAYOUT.getPredefinedNamedArgumentsMap(),
+ new StringToIndexMap.Entry(DISABLED_PARAM_NAME, DISABLED_PARAM_IDX)
+ ),
+ true);
+
+ private boolean disabled;
+
+ protected HiddenInputTemplateDirectiveModel(HttpServletRequest request, HttpServletResponse response) {
+ super(request, response);
+ }
+
+ @Override
+ public ArgumentArrayLayout getDirectiveArgumentArrayLayout() {
+ return ARGS_LAYOUT;
+ }
+
+ @Override
+ public boolean isNestedContentSupported() {
+ return false;
+ }
+
+ @Override
+ protected void executeInternal(TemplateModel[] args, CallPlace callPlace, Writer out, Environment env,
+ ObjectWrapperAndUnwrapper objectWrapperAndUnwrapper, RequestContext requestContext)
+ throws TemplateException, IOException {
+ super.executeInternal(args, callPlace, out, env, objectWrapperAndUnwrapper, requestContext);
+
+ disabled = CallableUtils.getOptionalBooleanArgument(args, DISABLED_PARAM_IDX, this, false);
+
+ TagOutputter tagOut = new TagOutputter(out);
+
+ tagOut.beginTag("input");
+
+ writeDefaultAttributes(tagOut);
+
+ // more optional attributes by this tag
+ tagOut.writeAttribute("type", "hidden");
+ if (isDisabled()) {
+ tagOut.writeAttribute(DISABLED_PARAM_NAME, "disabled");
+ }
+
+ String value = getDisplayString(getBindStatus().getValue(), getBindStatus().getEditor());
+ tagOut.writeAttribute("value", processFieldValue(env, getName(), value, "hidden"));
+
+ tagOut.endTag();
+
+ }
+
+ public boolean isDisabled() {
+ return disabled;
+ }
+
+ @Override
+ protected boolean isValidDynamicAttribute(String localName, Object value) {
+ return !"type".equals(localName);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/SpringFormTemplateCallableHashModel.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/SpringFormTemplateCallableHashModel.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/SpringFormTemplateCallableHashModel.java
index eefc9cc..11d5e33 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/SpringFormTemplateCallableHashModel.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/SpringFormTemplateCallableHashModel.java
@@ -48,6 +48,7 @@ public final class SpringFormTemplateCallableHashModel implements TemplateHashMo
modelsMap.put(FormTemplateDirectiveModel.NAME, new FormTemplateDirectiveModel(request, response));
modelsMap.put(InputTemplateDirectiveModel.NAME, new InputTemplateDirectiveModel(request, response));
modelsMap.put(PasswordInputTemplateDirectiveModel.NAME, new PasswordInputTemplateDirectiveModel(request, response));
+ modelsMap.put(HiddenInputTemplateDirectiveModel.NAME, new HiddenInputTemplateDirectiveModel(request, response));
modelsMap.put(TextareaTemplateDirectiveModel.NAME, new TextareaTemplateDirectiveModel(request, response));
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/TextareaTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/TextareaTemplateDirectiveModel.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/TextareaTemplateDirectiveModel.java
index 0b4fb85..9de22f9 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/TextareaTemplateDirectiveModel.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/TextareaTemplateDirectiveModel.java
@@ -53,7 +53,7 @@ import org.springframework.web.servlet.support.RequestContext;
* <@form.textarea 'user.description' rows="10" cols="80" />
* </PRE>
* <P>
- * <EM>Note:</EM> Unlike Spring Framework's <code><form:input /></code> JSP Tag Library, this directive
+ * <EM>Note:</EM> Unlike Spring Framework's <code><form:textarea /></code> JSP Tag Library, this directive
* does not support <code>htmlEscape</code> parameter. It always renders HTML's without escaping
* because it is much easier to control escaping in FreeMarker Template expressions.
* </P>
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModelTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModelTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModelTest.java
new file mode 100644
index 0000000..719a7c2
--- /dev/null
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/HiddenInputTemplateDirectiveModelTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.model.form;
+
+import org.apache.freemarker.spring.example.mvc.users.User;
+import org.apache.freemarker.spring.example.mvc.users.UserRepository;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration("classpath:META-INF/web-resources")
+@ContextConfiguration(locations = { "classpath:org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml" })
+public class HiddenInputTemplateDirectiveModelTest {
+
+ @Autowired
+ private WebApplicationContext wac;
+
+ @Autowired
+ private UserRepository userRepository;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setUp() {
+ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
+ }
+
+ @Test
+ public void testBasicUsages() throws Exception {
+ final Long userId = userRepository.getUserIds().iterator().next();
+ final User user = userRepository.getUser(userId);
+ mockMvc.perform(get("/users/{userId}/", userId).param("viewName", "test/model/form/hidden-input-directive-usages")
+ .accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
+ .andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+ .andExpect(xpath("//form[@id='form1']//input[@type='hidden' and @name='email']/@value").string(user.getEmail()))
+ .andExpect(xpath("//form[@id='form2']//input[@type='hidden' and @name='email']/@disabled").string("disabled"))
+ .andExpect(xpath("//form[@id='form2']//input[@type='hidden' and @name='email']/@value").string(user.getEmail()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/PasswordInputTemplateDirectiveModelTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/PasswordInputTemplateDirectiveModelTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/PasswordInputTemplateDirectiveModelTest.java
index d69adf2..4828e39 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/PasswordInputTemplateDirectiveModelTest.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/PasswordInputTemplateDirectiveModelTest.java
@@ -64,8 +64,8 @@ public class PasswordInputTemplateDirectiveModelTest {
mockMvc.perform(get("/users/{userId}/", userId).param("viewName", "test/model/form/password-input-directive-usages")
.accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
- .andExpect(xpath("//form[@id='form1']//input[@name='password']/@value").string(""))
- .andExpect(xpath("//form[@id='form2']//input[@name='password']/@value").string(user.getPassword()))
- .andExpect(xpath("//form[@id='form3']//input[@name='password']/@value").string(""));
+ .andExpect(xpath("//form[@id='form1']//input[@type='password' and @name='password']/@value").string(""))
+ .andExpect(xpath("//form[@id='form2']//input[@type='password' and @name='password']/@value").string(user.getPassword()))
+ .andExpect(xpath("//form[@id='form3']//input[@type='password' and @name='password']/@value").string(""));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6bfbfd38/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/hidden-input-directive-usages.ftlh
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/hidden-input-directive-usages.ftlh b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/hidden-input-directive-usages.ftlh
new file mode 100644
index 0000000..633956d
--- /dev/null
+++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/hidden-input-directive-usages.ftlh
@@ -0,0 +1,63 @@
+<#--
+ 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.
+-->
+<html>
+<body>
+
+ <h1>Form 1</h1>
+ <hr/>
+ <form id="form1">
+ <table>
+ <tr>
+ <th>Hidden1 :</th>
+ <td>
+ <@form.hidden 'user.email' />
+ </td>
+ </tr>
+ </table>
+ </form>
+
+ <hr/>
+
+ <form id="form2">
+ <table>
+ <tr>
+ <th>Hidden 2:</th>
+ <td>
+ <@form.hidden 'user.email' disabled=true />
+ </td>
+ </tr>
+ </table>
+ </form>
+
+ <hr/>
+
+ <h2>Testing with setting showPassword to false explicitly</h2>
+ <form id="form3">
+ <table>
+ <tr>
+ <th>Password:</th>
+ <td>
+ <@form.password 'user.password' showPassword=false />
+ </td>
+ </tr>
+ </table>
+ </form>
+
+</body>
+</html>