<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4199377741773992283</id><updated>2012-02-16T12:15:16.678+01:00</updated><category term='grails'/><category term='service'/><category term='session'/><title type='text'>Robert Bjærum's Blog</title><subtitle type='html'>Notes on Software Development, Java, ActorFrame, Grails, ...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://robertbjarum.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4199377741773992283/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://robertbjarum.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Robert Bjærum</name><uri>http://www.blogger.com/profile/10696346215861665452</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4199377741773992283.post-5848884522798592178</id><published>2008-10-21T18:29:00.004+02:00</published><updated>2008-10-21T19:35:57.771+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='service'/><category scheme='http://www.blogger.com/atom/ns#' term='session'/><category scheme='http://www.blogger.com/atom/ns#' term='grails'/><title type='text'>Accessing Session from a Grails Service</title><content type='html'>&lt;span style="font-family:arial;"&gt;Grails makes the session object (instance of the Servlet API's HttpSession class) directly available in the Controllers and Tag Libraries.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Furthermore, the Grails implementation of HttpSession allows you to access the session as a Groovy map.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;session.user = "Robert"&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;will store a user name in the session's attribute list.&lt;br /&gt;&lt;br /&gt;I wanted to encapsulate session data in a service and avoid defining session properties many places.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;However, the session is not directly available in a Grails Service.&lt;/span&gt; So if you want to access the session from a Service, you do it via the RequestContextHolder class (part of the Spring framework).&lt;br /&gt;&lt;br /&gt;Here is simple Service that does exactly this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;import javax.servlet.http.HttpSession&lt;br /&gt;import org.springframework.web.context.request.RequestContextHolder&lt;br /&gt;&lt;br /&gt;class SessionStorageService {&lt;br /&gt;&lt;br /&gt;  static transactional = false&lt;br /&gt;  static scope = "singleton"&lt;br /&gt;&lt;br /&gt;  def setUser(User user) {&lt;br /&gt;    getSession().user = user&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  def getUser() {&lt;br /&gt;    getSession().user&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private HttpSession getSession() {&lt;br /&gt;    return RequestContextHolder.currentRequestAttributes().getSession()&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;This service is made available in Controllers by automatic dependency injection by placing a property in the controller with the name &lt;code&gt;sessionStorageService&lt;/code&gt;:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;class BaseController {&lt;br /&gt;  def sessionStorageService&lt;br /&gt;  ...&lt;br /&gt;  sessionStorageService.setUser("Robert")&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;Any comments or suggestions are appreciated .&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4199377741773992283-5848884522798592178?l=robertbjarum.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://robertbjarum.blogspot.com/feeds/5848884522798592178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4199377741773992283&amp;postID=5848884522798592178' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4199377741773992283/posts/default/5848884522798592178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4199377741773992283/posts/default/5848884522798592178'/><link rel='alternate' type='text/html' href='http://robertbjarum.blogspot.com/2008/10/accessing-session-httpsession-from.html' title='Accessing Session from a Grails Service'/><author><name>Robert Bjærum</name><uri>http://www.blogger.com/profile/10696346215861665452</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4199377741773992283.post-1809118813096753229</id><published>2008-09-29T12:27:00.004+02:00</published><updated>2008-09-29T15:42:24.253+02:00</updated><title type='text'>First Blog ...</title><content type='html'>Primarily a Java Developer's Blog. I will be using this to share my experience with software development technologies and as a notebook to remember ...&lt;br /&gt;&lt;br /&gt;Keywords are: Grails, Groovy, Java, Hibernate, Spring, ActorFrame&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4199377741773992283-1809118813096753229?l=robertbjarum.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://robertbjarum.blogspot.com/feeds/1809118813096753229/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4199377741773992283&amp;postID=1809118813096753229' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4199377741773992283/posts/default/1809118813096753229'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4199377741773992283/posts/default/1809118813096753229'/><link rel='alternate' type='text/html' href='http://robertbjarum.blogspot.com/2008/09/first-blog.html' title='First Blog ...'/><author><name>Robert Bjærum</name><uri>http://www.blogger.com/profile/10696346215861665452</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
