<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog::Log &#187; spring</title>
	<atom:link href="http://www.stpe.se/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stpe.se</link>
	<description>Not a blog, more a log.</description>
	<lastBuildDate>Wed, 02 Jun 2010 13:01:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Date fields in forms using Spring and AppFuse</title>
		<link>http://www.stpe.se/2008/07/date-fields-in-forms-using-spring-and-appfuse/</link>
		<comments>http://www.stpe.se/2008/07/date-fields-in-forms-using-spring-and-appfuse/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 08:50:14 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[appfuse]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.stpe.se/?p=24</guid>
		<description><![CDATA[I had some problems getting date fields to work (and return a friendly error message) so here comes a little documentation for future reference. This was in an AppFuse based Spring Framework project. The model contains a java.util.Date property and simply exposing it as usual in a form will cause an exception since Spring doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I had some problems getting date fields to work (and return a friendly error message) so here comes a little documentation for future reference. This was in an AppFuse based Spring Framework project.</p>
<p>The model contains a java.util.Date property and simply exposing it as usual in a form will cause an exception since Spring doesn&#8217;t know how to convert the string into a Date:</p>
<blockquote><p>Failed to convert property value of type  to required type  for property startTime; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type  to required type  for property startTime: no matching editors or conversion strategy found</p></blockquote>
<p>To solve this we need a custom property editor. It may be registered by overriding the initBinder() method of a controller. Spring already contains a CustomDateEditor that we may use for this purpose.</p>
<pre class="brush: java;">
protected void initBinder(HttpServletRequest request,
                          ServletRequestDataBinder binder) {
    SimpleDateFormat dateFormat =
        new SimpleDateFormat(getText(&quot;date.format&quot;, request.getLocale()));
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}</pre>
<p>Note that you may replace the getText() call with a hard-coded &#8220;yyyy-MM-dd&#8221; or similar, at least for debugging purposes.</p>
<p>If it is an AppFuse project and the controller extends BaseFormController then you don&#8217;t need this since the CustomDateEditor is already registered in the BaseFormController.initBinder() method.</p>
<p>Now it should work, however, the error message isn&#8217;t especially user-friendly when entering an invalid date:</p>
<blockquote><p>Failed to convert property value of type  to required type  for property startTime; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: &#8220;2008-37-09&#8243;</p></blockquote>
<p>Better error messages may be provided using the resource file of the application (make sure you have a ResourceBundleMessageSource set up) by specifying typeMismatch error message for this type:</p>
<blockquote><p>typeMismatch.java.util.Date={0} is an invalid date.</p></blockquote>
<p>The {0} will be replaced by the property name, in this case startTime, which may not be desired. This may be overridden by setting a custom error message for this specific property:</p>
<blockquote><p>typeMismatch.startTime=Start time is an invalid date.</p></blockquote>
<p>Do also make sure to update validation.xml accordingly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stpe.se/2008/07/date-fields-in-forms-using-spring-and-appfuse/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I18n workaround for Spring form:options tag</title>
		<link>http://www.stpe.se/2008/07/i18n-workaround-for-spring-formoptions-tag/</link>
		<comments>http://www.stpe.se/2008/07/i18n-workaround-for-spring-formoptions-tag/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 04:53:28 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.stpe.se/?p=22</guid>
		<description><![CDATA[Spring Framework provides the convenient form:options tag to render a list of &#60;option&#62; tags for a form &#60;select&#62;. Here is an example: &#60;form:select id=&#34;status&#34; path=&#34;status&#34; cssClass=&#34;text large&#34; cssErrorClass=&#34;text large error&#34; &#62; &#60;form:options items=&#34;${statusList}&#34; itemValue=&#34;id&#34; itemLabel=&#34;label&#34;&#62;&#60;/form:options&#62; &#60;/form:select&#62; It does iteratate through statusList and will output each item as an option with the value statusList[i].id and the [...]]]></description>
			<content:encoded><![CDATA[<p>Spring Framework provides the convenient <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring-form.tld.html#spring-form.tld.options">form:options</a> tag to render a list of &lt;option&gt; tags for a form &lt;select&gt;. Here is an example:</p>
<pre class="brush: java;">
&lt;form:select id=&quot;status&quot; path=&quot;status&quot; cssClass=&quot;text large&quot; cssErrorClass=&quot;text large error&quot; &gt;
    &lt;form:options items=&quot;${statusList}&quot; itemValue=&quot;id&quot; itemLabel=&quot;label&quot;&gt;&lt;/form:options&gt;
&lt;/form:select&gt;
</pre>
<p>It does iteratate through <strong>statusList</strong> and will output each item as an option with the value <strong>statusList[i].id</strong> and the text <strong>statusList[i].label</strong> and automatically make the option corresponding to <strong>status</strong> selected.</p>
<p>Unfortunately form:options does not support i18n (internationalization), which make it in some cases rather worthless. I18n support is <a href="http://jira.springframework.org/browse/SPR-2659">currently scheduled</a> for inclusion in 3.0 M1.</p>
<p>To support internationalization we have to do a workaround that is somewhat more clunky:</p>
<pre class="brush: java;">
&lt;select id=&quot;status&quot; name=&quot;status&quot; class=&quot;text large&quot;&gt;
  &lt;c:set var=&quot;currentStatus&quot;&gt;${currentStatus}&lt;/c:set&gt;
  &lt;c:forEach var=&quot;status&quot; items=&quot;${statusList}&quot;&gt;
    &lt;c:choose&gt;
      &lt;c:when test=&quot;${status == currentStatus}&quot;&gt;
        &lt;option value=&quot;${status.id}&quot; selected&gt;&lt;fmt:message key=&quot;status.${status.label}&quot;/&gt;&lt;/option&gt;
      &lt;/c:when&gt;
      &lt;c:otherwise&gt;
        &lt;option value=&quot;${status.id}&quot;&gt;&lt;fmt:message key=&quot;status.${status.label} quot;/&gt;&lt;/option&gt;
      &lt;/c:otherwise&gt;
    &lt;/c:choose&gt;
  &lt;/c:forEach&gt;
&lt;/select&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stpe.se/2008/07/i18n-workaround-for-spring-formoptions-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
