<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=eberhardwolff" rel="nofollow">Eberhard Wolff</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5001?redirect=false" rel="nofollow">SPR-5001</a></strong> and commented</p>
<p dir="auto">Currently a parameter to a Web MVC method can be annotated using <code class="notranslate">@RequestMapping</code> so that a part of the HTTP request is bound to this parameter. I would suggest to add <code class="notranslate">@SessionMapping</code> that allows the same for the HTTP Session.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398189020" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/18468" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/18468/hovercard" href="https://github.com/spring-projects/spring-framework/issues/18468">#18468</a> Convenient access to session and request attributes in controller methods (<em><strong>"is superseded by"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=scottland" rel="nofollow">Scott Murphy</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5377?redirect=false" rel="nofollow">SPR-5377</a></strong> and commented</p>
<p dir="auto">Add an additional Annotation for passing objects in the session to a function.  Currently it is a little cumbersome to do this because you have to pass the HttpServletRequest object to your function even if that is all you use it for.</p>
<p dir="auto">For example:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@RequestMapping(&quot;/myController.do&quot;)
public String doGet(HttpServletRequest request) {
    MyObject object = (MyObject) WebUtils.getSessionAttribute(request, &quot;myObject&quot;);
    return &quot;MyJSP&quot;;
}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s">"/myController.do"</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-s1">doGet</span>(<span class="pl-smi">HttpServletRequest</span> <span class="pl-s1">request</span>) {
    <span class="pl-smi">MyObject</span> <span class="pl-s1">object</span> = (<span class="pl-smi">MyObject</span>) <span class="pl-smi">WebUtils</span>.<span class="pl-en">getSessionAttribute</span>(<span class="pl-s1">request</span>, <span class="pl-s">"myObject"</span>);
    <span class="pl-k">return</span> <span class="pl-s">"MyJSP"</span>;
}</pre></div>
<p dir="auto">Should be replaced with:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@RequestMapping(&quot;/myController.do&quot;)
public String doGet(@SessionParam(&quot;myObject&quot;) MyObject myObject) {
    return &quot;MyJSP&quot;;
}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s">"/myController.do"</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-s1">doGet</span>(<span class="pl-c1">@</span><span class="pl-c1">SessionParam</span>(<span class="pl-s">"myObject"</span>) <span class="pl-smi">MyObject</span> <span class="pl-s1">myObject</span>) {
    <span class="pl-k">return</span> <span class="pl-s">"MyJSP"</span>;
}</pre></div>
<p dir="auto">If the object is not in the session, null is passed.</p>
<p dir="auto">Furthermore, an additional boolean parameter can be supplied to initialize the object and put it in the Session if it is not already there:<br>
e.g.:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@RequestMapping(&quot;/myController.do&quot;)
public String doGet(@SessionParam(value=&quot;myObject&quot;, initialize=true) MyObject myObject) {
    return &quot;MyJSP&quot;;
}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s">"/myController.do"</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-s1">doGet</span>(<span class="pl-c1">@</span><span class="pl-c1">SessionParam</span>(<span class="pl-s1">value</span>=<span class="pl-s">"myObject"</span>, <span class="pl-s1">initialize</span>=<span class="pl-c1">true</span>) <span class="pl-smi">MyObject</span> <span class="pl-s1">myObject</span>) {
    <span class="pl-k">return</span> <span class="pl-s">"MyJSP"</span>;
}</pre></div>
<p dir="auto">If myObject is not in the session, a MyObject object will be created and placed in the Session.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0 M1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398166507" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/16171" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/16171/hovercard" href="https://github.com/spring-projects/spring-framework/issues/16171">#16171</a> Provide <code class="notranslate">@ModelAttribute</code>(required="false") for session attributes</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398189020" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/18468" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/18468/hovercard" href="https://github.com/spring-projects/spring-framework/issues/18468">#18468</a> Convenient access to session and request attributes in controller methods (<em><strong>"is superseded by"</strong></em>)</li>
</ul>
<p dir="auto">4 votes, 4 watchers</p>