- Revision
- 613
- Author
- mauro
- Date
- 2008-04-14 12:55:45 -0500 (Mon, 14 Apr 2008)
Log Message
Added DateValueConverter to bind converters. Made ListValueConverter depend on MessageResources to allow i18n. Updated freemarker-example to use the date value converter.
Modified Paths
- trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml
- trunk/examples/freemarker-example/src/main/webapp/people/editperson.htm
- trunk/examples/freemarker-example/src/main/webapp/people/person.htm
- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java
- trunk/waffle-core/src/main/java/org/codehaus/waffle/i18n/DefaultMessageResources.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/OgnlDataBinderTest.java
Added Paths
Diff
Modified: trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml (612 => 613)
--- trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/examples/freemarker-example/src/main/webapp/WEB-INF/web.xml 2008-04-14 17:55:45 UTC (rev 613) @@ -17,10 +17,13 @@ <param-value>org.codehaus.waffle.action.ParanamerMethodDefinitionFinder</param-value> </context-param> <context-param> + <param-name>register:DateValueConverter</param-name> + <param-value>org.codehaus.waffle.bind.converters.DateValueConverter</param-value> + </context-param> + <context-param> <param-name>register:ListValueConverter</param-name> <param-value>org.codehaus.waffle.bind.converters.ListValueConverter</param-value> </context-param> - <!-- 3. Waffle context listener (ServletContext and HttpSession) --> <listener>
Modified: trunk/examples/freemarker-example/src/main/webapp/people/editperson.htm (612 => 613)
--- trunk/examples/freemarker-example/src/main/webapp/people/editperson.htm 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/examples/freemarker-example/src/main/webapp/people/editperson.htm 2008-04-14 17:55:45 UTC (rev 613) @@ -38,12 +38,11 @@ <@w.selectMultiple "person.skills" controller.getSkills() person.getSkills() "size='5'"/> <br style="clear:both"/> </div> - <!-- TODO fix freemarker exception when submitting date <div class="fieldRow"> <label for="" Of Birth:</label> - <input type="text" name="person.dateOfBirth" id="person.dateOfBirth" value="${person.dateOfBirth?string("dd-MM-yyyy")}"/> + <@w.text "person.dateOfBirth" "${person.dateOfBirth?string('dd/MM/yyyy')}"/> <br style="clear:both"/> - </div>--> + </div> <br/> <a href="" |
Modified: trunk/examples/freemarker-example/src/main/webapp/people/person.htm (612 => 613)
--- trunk/examples/freemarker-example/src/main/webapp/people/person.htm 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/examples/freemarker-example/src/main/webapp/people/person.htm 2008-04-14 17:55:45 UTC (rev 613) @@ -38,7 +38,7 @@ <td>${person.firstName}</td> <td>${person.lastName}</td> <td>${person.email}</td> - <td>${person.dateOfBirth?string("dd-MM-yyyy")}</td> + <td>${person.dateOfBirth?string("dd/MM/yyyy")}</td> <td><@w.asCSV person.getSkills() /> </p></td> <td><@w.checkbox "selectedIds" "${person.id}" /></td> </tr>
Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java (0 => 613)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java 2008-04-14 17:55:45 UTC (rev 613) @@ -0,0 +1,57 @@ +/***************************************************************************** + * Copyright (c) 2005-2008 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Michael Ward * + *****************************************************************************/ +package org.codehaus.waffle.bind.converters; + +import org.codehaus.waffle.bind.BindException; +import org.codehaus.waffle.bind.ValueConverter; +import org.codehaus.waffle.i18n.MessageResources; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * <code>ValueConverter</code> that converts Date values. + * A <code>null</code>, empty or invalid value will cause a BindException to be thrown. + * + * @author Michael Ward + * @author Mauro Talevi + */ +public class DateValueConverter implements ValueConverter { + private final MessageResources messageResources; + + public DateValueConverter(MessageResources messageResources) { + this.messageResources = messageResources; + } + + public boolean accept(Class<?> type) { + return Date.class.isAssignableFrom(type); + } + + @SuppressWarnings({"unchecked"}) + public <T> T convertValue(String propertyName, String value, Class<T> toType) { + String fieldName = messageResources.getMessageWithDefault(propertyName, propertyName); + if (value == null || value.equals("")) { + String message = messageResources.getMessageWithDefault("bind.error.date.missing", "Missing date value for field ", fieldName); + throw new BindException(message); + } + + String datePattern = messageResources.getMessageWithDefault("date.format", "dd/MM/yyyy"); + + try { + return (T) new SimpleDateFormat(datePattern).parse(value); + } catch (ParseException e) { + String message = messageResources.getMessageWithDefault("bind.error.date.invalid", "Date {1} invalid for field {0} with pattern {2}", fieldName, value, datePattern); + throw new BindException(message); + } + } + +}
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java (612 => 613)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java 2008-04-14 17:55:45 UTC (rev 613) @@ -6,7 +6,7 @@ * style license a copy of which has been included with this distribution in * * the LICENSE.txt file. * * * - * Original code by: Mauro Talevi * + * Original code by: Michael Ward * *****************************************************************************/ package org.codehaus.waffle.bind.converters; @@ -16,23 +16,33 @@ import org.codehaus.waffle.bind.BindException; import org.codehaus.waffle.bind.ValueConverter; +import org.codehaus.waffle.i18n.MessageResources; /** - * <code>ValueConverter</code> that converts a CSV value to a List of Strings. + * <code>ValueConverter</code> that converts a CSV value to a List of Strings. * A <code>null</code> value will cause a BindException to thrown. - * + * * @author Mauro Talevi */ public class ListValueConverter implements ValueConverter { + private final MessageResources messageResources; + + public ListValueConverter(MessageResources messageResources) { + this.messageResources = messageResources; + } + public boolean accept(Class<?> type) { return List.class.isAssignableFrom(type); } - @SuppressWarnings({"unchecked"}) + @SuppressWarnings( { "unchecked" }) public <T> T convertValue(String propertyName, String value, Class<T> toType) { - if ( value == null ){ - throw new BindException("Cannot convert null value for property "+propertyName); + if (value == null) { + String fieldName = messageResources.getMessageWithDefault(propertyName, propertyName); + String message = messageResources.getMessageWithDefault("bind.error.list.missing", + "Missing list value for field {0} for list {1}", fieldName); + throw new BindException(message); } return (T) asList(value.split(",")); }
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/i18n/DefaultMessageResources.java (612 => 613)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/i18n/DefaultMessageResources.java 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/i18n/DefaultMessageResources.java 2008-04-14 17:55:45 UTC (rev 613) @@ -10,14 +10,18 @@ *****************************************************************************/ package org.codehaus.waffle.i18n; -import java.text.MessageFormat; +import static java.text.MessageFormat.format; +import static java.util.ResourceBundle.getBundle; + import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; /** - * + * Default ResourceBundle-based implementation of MessageResorces. + * * @author Michael Ward + * @author Mauro Talevi */ public class DefaultMessageResources implements MessageResources { private final static ThreadLocal<Locale> userLocale = new ThreadLocal<Locale>(); @@ -42,17 +46,15 @@ } public String getMessage(String key, Object ... arguments) { - ResourceBundle resourceBundle = ResourceBundle - .getBundle(bundleName, userLocale.get()); - String message = resourceBundle.getString(key); - return MessageFormat.format(message, arguments); + ResourceBundle resourceBundle = getBundle(bundleName, userLocale.get()); + return format(resourceBundle.getString(key), arguments); } public String getMessageWithDefault(String key, String defaultValue, Object ... arguments) { try { - return this.getMessage(key, arguments); + return getMessage(key, arguments); } catch (MissingResourceException e) { - return defaultValue; + return format(defaultValue, arguments); } }
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java (612 => 613)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-04-14 17:55:45 UTC (rev 613) @@ -10,6 +10,7 @@ import org.codehaus.waffle.bind.ValueConverter; import org.codehaus.waffle.bind.converters.ListValueConverter; import org.codehaus.waffle.context.ContextLevel; +import org.codehaus.waffle.i18n.DefaultMessageResources; import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.integration.junit4.JMock; @@ -44,7 +45,7 @@ @Test public void canDelegateToListValueConverter() { - final ValueConverter valueConverter = new ListValueConverter(); + final ValueConverter valueConverter = new ListValueConverter(new DefaultMessageResources()); final List<String> list = asList("one","two"); DelegatingTypeConverter converter = new DelegatingTypeConverter(new OgnlValueConverterFinder(valueConverter)); @@ -54,7 +55,7 @@ @Test(expected=BindException.class) public void cannotDelegateToListValueConverterNullValue() { - final ValueConverter valueConverter = new ListValueConverter(); + final ValueConverter valueConverter = new ListValueConverter(new DefaultMessageResources()); DelegatingTypeConverter converter = new DelegatingTypeConverter(new OgnlValueConverterFinder(valueConverter)); converter.convertValue("propertyName", null, List.class);
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/OgnlDataBinderTest.java (612 => 613)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/OgnlDataBinderTest.java 2008-04-14 16:45:10 UTC (rev 612) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/OgnlDataBinderTest.java 2008-04-14 17:55:45 UTC (rev 613) @@ -18,6 +18,7 @@ import org.codehaus.waffle.bind.DataBinder; import org.codehaus.waffle.bind.converters.ListValueConverter; import org.codehaus.waffle.context.ContextLevel; +import org.codehaus.waffle.i18n.DefaultMessageResources; import org.codehaus.waffle.monitor.SilentMonitor; import org.codehaus.waffle.testmodel.FakeBean; import org.codehaus.waffle.testmodel.FakeController; @@ -90,7 +91,7 @@ }); FakeController fakeController = new FakeController(); - DataBinder binder = new OgnlDataBinder(new OgnlValueConverterFinder(new ListValueConverter()), null, new SilentMonitor()); + DataBinder binder = new OgnlDataBinder(new OgnlValueConverterFinder(new ListValueConverter(new DefaultMessageResources())), null, new SilentMonitor()); ErrorsContext errorsContext = new DefaultErrorsContext(null); binder.bind(request, null, errorsContext, fakeController);
To unsubscribe from this list please visit:
