Spring MVC 3.2 将支持 Servlet 3 异步请求
jopen 12年前
<p>来自 Spring 官方<a href="/misc/goto?guid=4958339216281527874" target="_blank">博客</a>的消息:</p> <p>Spring MVC 3.2 M1 将引入对异步请求处理的支持,基于 Servlet 3.0 规范。尽管该版本还没发布,你可以在<a href="/misc/goto?guid=4958339217078797143" target="_blank">这里</a>看到包含的改进。</p> <p><strong>简单预览一下:</strong></p> <p>一个控制器方法可通过返回 Callable 来完成异步处理:</p> <div id="highlighter_351428" class="syntaxhighlighter nogutter "> <div class="lines no-wrap"> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="color1">@RequestMapping</code><code class="plain">(method=RequestMethod.POST)</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="keyword">public</code> <code class="plain">Callable<String> processUpload(</code><code class="keyword">final</code> <code class="plain">MultipartFile file) {</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"> </td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="keyword">return</code> <code class="keyword">new</code> <code class="plain">Callable<String>() {</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="keyword">public</code> <code class="plain">Object call() </code><code class="keyword">throws</code> <code class="plain">Exception {</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="comments">// ...</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="keyword">return</code> <code class="string">"someView"</code><code class="plain">;</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="plain">}</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="plain">};</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="plain">}</code></td> </tr> </tbody> </table> </div> </div> </div> <p>或者控制器方法返回 <code>DeferredResult</code> (new in Spring 3.2 M1) 来完成处理</p> <div id="highlighter_832667" class="syntaxhighlighter nogutter "> <div class="lines no-wrap"> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="color1">@RequestMapping</code><code class="plain">(</code><code class="string">"/quotes"</code><code class="plain">)</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="color1">@ResponseBody</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="keyword">public</code> <code class="plain">DeferredResult quotes() {</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="plain">DeferredResult deferredResult = </code><code class="keyword">new</code> <code class="plain">DeferredResult();</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="comments">// Add deferredResult to a Queue or a Map...</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="spaces"> </code><code class="keyword">return</code> <code class="plain">deferredResult;</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="plain">}</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"> </td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="comments">// In some other thread..</code></td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="comments">// Set the return value on the deferredResult</code></td> </tr> </tbody> </table> </div> <div class="line alt1"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"> </td> </tr> </tbody> </table> </div> <div class="line alt2"> <table class="ke-zeroborder"> <tbody> <tr> <td class="content"><code class="plain">deferredResult.set(data);</code></td> </tr> </tbody> </table> </div> </div> </div> <p> </p> <p>更多关于 Spring MVC 3.2 新特性以及详细说明请看<a href="/misc/goto?guid=4958339216281527874" target="_blank">这里</a>。</p>