<?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>Cake Solutions Team Blog &#187; spring mvc</title>
	<atom:link href="http://www.cakesolutions.net/teamblogs/tag/spring-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cakesolutions.net/teamblogs</link>
	<description>void magic() { }</description>
	<lastBuildDate>Tue, 07 Feb 2012 12:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>UTF-8 encoding revisited</title>
		<link>http://www.cakesolutions.net/teamblogs/2011/11/08/utf-8-encoding-revisited/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=utf-8-encoding-revisited</link>
		<comments>http://www.cakesolutions.net/teamblogs/2011/11/08/utf-8-encoding-revisited/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 09:14:10 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[Internationalisation]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=1187</guid>
		<description><![CDATA[My old post on UTF-8 encoding in Spring messages using the ReloadableResourceBundleMessageSource still receives a lot of attention. However, UTF-8 encoding of the messages is just a part of i18n applications. (It&#8217;s called i18n because the word is internationalisation&#8211;i, followed &#8230; <a href="http://www.cakesolutions.net/teamblogs/2011/11/08/utf-8-encoding-revisited/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My old post on UTF-8 encoding in Spring messages using the <code>ReloadableResourceBundleMessageSource</code> still receives a lot of attention. However, UTF-8 encoding of the messages is just a part of i18n applications. (It&#8217;s called i18n because the word is i<em>nternationalisatio</em>n&#8211;<em>i</em>, followed by 18 letters, followed by <em>n</em>&#8211;did you know?)<br />
Now, the Spring <code>ReloadableResourceBundleMessageSource</code> is usually declared as </p>
<pre class="brush:[xml]">&lt;bean id="messageSource"
      class="org.springframework.context.support.&#x21A9
             ReloadableResourceBundleMessageSource"&gt;
    &lt;property name="basename" value="classpath:messages"/&gt;
    &lt;property name="defaultEncoding" value="UTF-8"/&gt;
&lt;/bean&gt;
</pre>
<p>The key bit here is, of course, the <code>defaultEncoding=UTF-8</code> property. This ensures that the <code>MessageSource</code>&#8216;s messages will not be mangled when they are loaded from the files. So far, so good! </p>
<h2>I18n web applications</h2>
<p>However, the <code>MessageSource</code>s are just one part of truly internationalisable web applications. It is very easy for the developers to forget that the messages in some langauges will be <em>much</em> longer than in English; also, some languages are written right-to-left (think Arabic, Hebrew). So, simply using the messages might not be enough for truly internationalisable application.</p>
<h3>Views, locales and LocaleResolvers</h3>
<p>In Spring MVC (and Spring Mobile), we define a sequence of <code>ViewResolver</code>s and <code>LocaleResolver</code>s, which together determine which view to display. The code in our controllers can remain oblivious of the users&#8217; locales:</p>
<pre class="brush:[scala]">@Controller
class UserController @Autowired() (private val userService: UserService) {

  @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
  def view(@PathVariable id: Long, model: Model) = {
    model.addAttribute(userService.findUserById(id))
    "users/view"
  }

}
</pre>
<p>(Notice that I&#8217;ve sneaked in Scala to our Spring application, but the Java code would look almost the same!) Now, the method <code>view</code> returns a <code>String</code>. That string now needs to be turned into a <code>View</code> and, together with the <code>model</code>, become the familiar <code>ModelAndView</code> object.<br />
The <code>ViewResolver</code> and <code>LocaleResolver</code> do just that. (It is more accurate to say that we have an ordered sequence of <code>ViewResolver</code>s and <code>LocaleResolver</code>s; and whichever <code>ViewResolver</code> or <code>LocaleResolver</code> first resolves the view or locale will be used.)<br />
We can include the messages loaded by the <code>MessageSource</code> in our views (say, JSPs for simlicity) using the familiar Spring forms tag library:</p>
<pre class="brush:[html]">&lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
&lt;html&gt;
&lt;head>…&lt;/head&gt;
&lt;body&gt;
&lt;spring:message code="edit-user"/&gt;: &lt;spring:message code="username"/&gt; ...
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>That is simple to see. But <em>where to put the JSP</em>? We can start by keeping it in the <em>the-webapp</em> <code>src/main/webapp/WEB-INF/views</code> followed by <code>users/view.jsp</code>. I&#8217;ve separated the path on purpose: we can see that there is some kind of <em>prefix</em> (<code>/WEB-INF/views</code>), followed by the view name, followed by some <em>suffix</em> (<code>.jsp</code>). That is, in fact the configuration of the typical <code>InternalResourceViewResolver</code>.</p>
<pre class="brush:[xml]">
&lt;bean class="org.springframework.web.servlet.view.&#x21A9
                InternalResourceViewResolver"
  p:prefix="/WEB-INF/views"
  p:suffix=".jsp"/&gt;
</pre>
<p>Excellent. Our controller code works, the messages are happily being loaded with the correct encoding. </p>
<h3>Wide internationalisation support</h3>
<p>Unfortunately, there is a serious flaw in the structure I showed above. The HTML from the JSP above is going to display</p>
<pre>Edit user: username …</pre>
<p> in English (assuming the <code>edit-user</code> says &#8220;Edit user&#8221; and <code>username</code> says &#8220;username&#8221; in our messages bundle). It will still work reasonably well in Czech, showing</p>
<pre>Upravit uživatele: uživatelské jméno …</pre>
<p> (again, assuming the correct translations in the messages bundle).<br />
But what will happen in Arabic? Or Hebrew? Sure, we can create the appropriate transalations in the messages bundle, but remember, Arabic is right-to-left. And what if the Arabic text is far longer than the English or Czech one?<br />
It turns out that you need to structure your views differently; and that you need to modify the chosen <code>ViewResolver</code> to take the new structure into account. At first, I thought that I should have directory structure under <code>WEB-INF/views</code> for every locale, with fallback rules ensuring that I end up using the <code>en_GB</code> locale in the end. But that was confusing. Why should the Czech localisation use JSPs from the <code>en_GB</code> directory?<br />
The most helpful structure I found is to have the following view structure under <code>WEB-INF/views</code>:</p>
<pre>
ltr
  user
    view.jsp
    edit.jsp
    add.jsp
  …
rtl
  user
    view.jsp
    edit.jsp
    add.jsp
  …
tr_TR
  user
    view.jsp
</pre>
<p>In this example, I have all files for the left-to-right languages and right-to-left languages and I also have a special case for the view user JSP in Turkish.</p>
<h2>Summary</h2>
<p>Such a structure gives you the most flexibility, and keeps the number of files and directories in your web applications in check. The only work that&#8217;s required on your part is the modification of your chosen implementation of the <code>ViewResolver</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2011/11/08/utf-8-encoding-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Guess what?</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/06/17/guess-what/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=guess-what</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/06/17/guess-what/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 20:25:07 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Open Spring 3]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=612</guid>
		<description><![CDATA[With the fantastic weather outside, I needed to take a break in my training. My legs felt very tired this morning and my heart rate just would not go above 155 bpm: all the signs to take a rest day. &#8230; <a href="http://www.cakesolutions.net/teamblogs/2010/06/17/guess-what/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With the fantastic weather outside, I needed to take a break in my training. My legs felt very tired this morning and my heart rate just would not go above 155 bpm: all the signs to take a rest day. Now, rest day for the legs does not mean rest day for the brain! And so, I put some more pages to the Beginning MVC chapter. Download <a href='http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/06/mvc11.pdf'>here</a>.</p>
<p>Oh, and one more thing. I spoke to Gregory from Mulesoft today and we had an interesting chat about OSGi. I will add my thoughts about this soon, in the meantime read Ross&#8217;s post at <a href="http://rossmason.blogspot.com/2008/04/osgi-is-irrelevant.html">http://rossmason.blogspot.com/2008/04/osgi-is-irrelevant.html</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/06/17/guess-what/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Open Spring 3 MVC goodies</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/06/15/more-open-spring-3-mvc-goodies/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=more-open-spring-3-mvc-goodies</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/06/15/more-open-spring-3-mvc-goodies/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:36:32 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Open Spring 3]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=609</guid>
		<description><![CDATA[I have been very busy indeed, but managed to find some time to add more text to the Beginning Spring MVC chapter of Open Spring 3. Get downloadin&#8217;!]]></description>
			<content:encoded><![CDATA[<p>I have been very busy indeed, but managed to find some time to add more text to the Beginning Spring MVC chapter of Open Spring 3. Get <a href='http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/06/mvc1.pdf'>downloadin&#8217;</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/06/15/more-open-spring-3-mvc-goodies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Spring 3 updated</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/04/28/open-spring-3-updated/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=open-spring-3-updated</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/04/28/open-spring-3-updated/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:48:11 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Open Spring 3]]></category>
		<category><![CDATA[Spring 3]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=600</guid>
		<description><![CDATA[Good news everyone! I&#8217;ve added a few pages to the Beginning Spring MVC chapter of the Open Spring 3 book. Download here for your reading pleasure!]]></description>
			<content:encoded><![CDATA[<p>Good news everyone! I&#8217;ve added a few pages to the Beginning Spring MVC chapter of the Open Spring 3 book. <a href='http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/04/mvc11.pdf'>Download here</a> for your reading pleasure!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/04/28/open-spring-3-updated/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Open Spring 3</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/04/03/open-spring-3/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=open-spring-3</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/04/03/open-spring-3/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 09:42:58 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Open Spring 3]]></category>
		<category><![CDATA[Pro Spring 3]]></category>
		<category><![CDATA[spring mvc]]></category>
		<category><![CDATA[The open series]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=581</guid>
		<description><![CDATA[Chapters for download are: Beginning Spring MVC I was thinking about the name of our new &#8220;unbook&#8221;. The &#8220;Pro&#8221; series are in the domain of Apress, the &#8220;In Action&#8221; books are published by Manning; I don&#8217;t know what O&#8217;Reilly call &#8230; <a href="http://www.cakesolutions.net/teamblogs/2010/04/03/open-spring-3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Chapters for download are:<br />
<a href='http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/04/mvc1.pdf'>Beginning Spring MVC</a></p>
<hr />
I was thinking about the name of our new &#8220;unbook&#8221;. The &#8220;Pro&#8221; series are in the domain of Apress, the &#8220;In Action&#8221; books are published by Manning; I don&#8217;t know what O&#8217;Reilly call their books, but I wanted to make sure I don&#8217;t step on anyone&#8217;s toes.<br />
In line with our advocacy of the open source, I decided to call the book <i>Open Spring 3</i>. I wanted to emphasise that it is a continuation of our Pro Spring series (in terms of scope and, I hope, quality and popularity), but that is is fundamentally different from the traditional printed books. And so, we give you Open Spring 3.<br />
Open Spring 3 is just the first one in what we hope will become a series of other books, all covering various open source technologies and all available for free (both as in speech and beer) online. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/04/03/open-spring-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC on the iPhone</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/03/25/spring-mvc-on-the-iphone/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-mvc-on-the-iphone</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/03/25/spring-mvc-on-the-iphone/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 16:03:34 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[OSUG]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=568</guid>
		<description><![CDATA[&#8230;and, as we discovered at the Open Source User Group meeting on 24th March, also on the iPod Touch! If you&#8217;d like to explore in detail the application I used yesterday, download it from GitHub. The webapp uses, as the &#8230; <a href="http://www.cakesolutions.net/teamblogs/2010/03/25/spring-mvc-on-the-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>&#8230;and, as we discovered at the Open Source User Group meeting on 24th March, also on the iPod Touch!<br />
If you&#8217;d like to explore in detail the application I used yesterday, download it from <a href="http://github.com/janm399/springmvc-iphone">GitHub</a>. The webapp uses, as the headline suggests, Spring MVC (with particular focus on <code>ViewResolver</code>s), and alongside the bog-standard JSP code, exploits <a href="http://www.jqtouch.com/">jQTouch</a> to make the iPhone/iPod Touch interface look very native app-like.<br />
Alternatively, if you don&#8217;t want to get messy with the source code, you can just download the <a href='http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/03/springmvc-iphone.pdf'>Spring MVC iPhone Slides</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/03/25/spring-mvc-on-the-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring and iPhone</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/03/15/spring-and-iphone/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-and-iphone</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/03/15/spring-and-iphone/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 11:36:07 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Open source user group]]></category>
		<category><![CDATA[spring mvc]]></category>
		<category><![CDATA[Talk]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=547</guid>
		<description><![CDATA[The talk for next week&#8217;s open source user group is ready. I will give brief overview of Spring 3 MVC and explain how to create native-looking iPhone web application using as little code as possible. It would not be a &#8230; <a href="http://www.cakesolutions.net/teamblogs/2010/03/15/spring-and-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The talk for next week&#8217;s open source user group is ready. I will give brief overview of Spring 3 MVC and explain how to create native-looking iPhone web application using as little code as possible.<br />
It would not be a proper user group talk without a live demo, so bring your good selves with your iPhones and we&#8217;ll create a Spring MVC application that looks just amazing on the iPhone, all within about 25 minutes!<br />
Figure 1 shows what we will achieve: the iPhone will send a request through the <a href="http://www.channel4.com/programmes/the-it-crowd">Internet</a> to our Tomcat, where our Spring-driven MVC application is going to process it.<br />
<a href="http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/03/smvci.png"><img src="http://www.cakesolutions.net/teamblogs/wp-content/uploads/2010/03/smvci-300x232.png" alt="smvci" title="smvci" width="300" height="232" class="aligncenter size-medium wp-image-548" /></a><br />
Figure 1. Spring MVC &#038; iPhone</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/03/15/spring-and-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring MVC and iPhone</title>
		<link>http://www.cakesolutions.net/teamblogs/2010/02/23/spring-mvc-and-iphone/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-mvc-and-iphone</link>
		<comments>http://www.cakesolutions.net/teamblogs/2010/02/23/spring-mvc-and-iphone/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 20:21:54 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[OSUG]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=528</guid>
		<description><![CDATA[I have exciting new talk ready for the next open source user group meeting. We will implement a very friendly iPhone web application using Spring MVC 3.0 in about 30 minutes&#8217; time while keeping the controllers and models completely intact. &#8230; <a href="http://www.cakesolutions.net/teamblogs/2010/02/23/spring-mvc-and-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have exciting new talk ready for the next open source user group meeting. We will implement a very friendly iPhone web application using Spring MVC 3.0 in about 30 minutes&#8217; time while keeping the controllers and models <i>completely</i> intact.<br />
I don&#8217;t want to spoil the show; come along and don&#8217;t forget to bring your iPhones for testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2010/02/23/spring-mvc-and-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom arguments for @RequestMapping methods</title>
		<link>http://www.cakesolutions.net/teamblogs/2009/10/23/custom-arguments-for-requestmapping-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=custom-arguments-for-requestmapping-methods</link>
		<comments>http://www.cakesolutions.net/teamblogs/2009/10/23/custom-arguments-for-requestmapping-methods/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 14:22:25 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[arguments for controller methods]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=480</guid>
		<description><![CDATA[So, Spring MVC fans, let&#8217;s say you have a @RequestMapping-annotated method in your controller, but you would like to include a custom argument, in addition to the standard ones like Model, HttpServletRequest, and HttpServletResponse. The solution is to implement a &#8230; <a href="http://www.cakesolutions.net/teamblogs/2009/10/23/custom-arguments-for-requestmapping-methods/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, Spring MVC fans, let&#8217;s say you have a <code>@RequestMapping</code>-annotated method in your controller, but you would like to include a custom argument, in addition to the standard ones like <code>Model</code>, <code>HttpServletRequest</code>, and <code>HttpServletResponse</code>.<br />
The solution is to implement a <code>WebArgumentResolver</code>. As an example, we&#8217;ll create a <code>CurrentDateWebArgumentResolver</code>; as its name suggests, this resolver will be able to set any <code>Date</code> argument in the controller method to current date.<br />
We have to write</p>
<pre>
public class ContextExtractingWebArgumentResolver
    implements WebArgumentResolver {

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
        NativeWebRequest webRequest) throws Exception {
        if (Date.class == methodParameter.getParameterType()) {
            return new Date();
        }
        return UNRESOLVED;
    }
}
</pre>
<p>We then wire-in the argument resolver in our Spring application context configuration file:</p>
<pre>
&lt;bean class="org.springframework.web.servlet.mvc.annotation.
      AnnotationMethodHandlerAdapter"
    &lt;property name="customArgumentResolver"&gt;
        &lt;bean class="package.ContextExtractingWebArgumentResolver"/&gt;
    &lt;/property&gt;
&lt;/bean&gt;
</pre>
<p>Once done, we can create a <code>@RequestMapping</code>-annotated method in our controller and include the Date argument; that argument will now receive the current date. For example, we can have</p>
<pre>
@Controller
public class HomeController {
    @RequestMapping(value="/index", method=RequestMethod.GET)
    public void index(Date date, Model m) {
        m.addAttribute("now", date);
    }
}
</pre>
<p>The model that will be passed to the view after making a GET request to the <code>/index</code> URL will include attribute named <code>now</code>; the value of the attribute will be the current time.<br />
Naturally, this is just a simple example and injecting current date is not very enterprisey thing to do, but using the approach I have outlined, you can now set much more useful argument types. Do not forget that the implementation of the <code>WebArgumentResolver</code> is a Spring bean, thus having access to all features available in the Spring application context.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2009/10/23/custom-arguments-for-requestmapping-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring MVC Volume II</title>
		<link>http://www.cakesolutions.net/teamblogs/2009/10/21/spring-mvc-volume-ii-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-mvc-volume-ii-2</link>
		<comments>http://www.cakesolutions.net/teamblogs/2009/10/21/spring-mvc-volume-ii-2/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 12:18:38 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Java User Group]]></category>
		<category><![CDATA[spring mvc]]></category>
		<category><![CDATA[Talk]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=478</guid>
		<description><![CDATA[It was my pleasure to deliver my Spring MVC talk at the Java User Group meeting in London yesterday. I loved the audience&#8211;they asked the right questions at the right time; at times they were paying too much attention and &#8230; <a href="http://www.cakesolutions.net/teamblogs/2009/10/21/spring-mvc-volume-ii-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It was my pleasure to deliver my Spring MVC talk at the Java User Group meeting in London yesterday. I loved the audience&#8211;they asked the right questions at the right time; at times they were paying too much attention and spotted a bug! Mea maxima culpa, all is corrected now.</p>
<p>The slides and the source  code are available at <a href="http://github.com/janm399/smvc/tree/master">http://github.com/janm399/smvc/tree/master</a>. </p>
<p>I&#8217;d love to hear suggestions for the next talk&#8211;I am thinking about advanced Spring MVC, where we take a look under the hood of the <a href="http://static.springsource.org/spring/docs/3.0.0.RC1/javadoc-api/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerAdapter.html">AnnotationMethodHandlerAdapter</a> and its friends. Alternatively, we could leave the cushy OO world behind and dive into AOP, with focus on [Spring|Dynamic] AOP.</p>
<p>Please comment or tweet to <a href="http://twitter.com/honzam399">@honzam399</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2009/10/21/spring-mvc-volume-ii-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

