Grails 中的 httpSession
我需要访问当前会话的域类 User 。以下代码有效:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = User.get(1)
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
但我需要访问当前会话的用户,而不是显式获取 ID 等于 1 的用户 (User.get(1))。我尝试了以下代码,但它不起作用:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = user.session
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
我仍在努力完全理解 httpSession 概念,所以一点帮助会很好。 提前致谢。
更新
在我的 UserController 中,我的身份验证如下所示:
def authenticate = {
def user = User.findByLoginAndPassword(params.login, params.password)
if(user){
session.user = user
flash.message = "Hello ${user.name}!"
redirect(controller:"event", action:"list")
}else{
flash.message = "Sorry, ${params.login}. Please try again."
redirect(action:"create")
}
}
I need to access the domain class User of the current session. The following code works:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = User.get(1)
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
But instead of explicitly get the user with ID equal 1 (User.get(1)) I need to access the user of the current session. I tried the following code, but it doesn't work:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = user.session
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
I'm still struggling to fully understand the httpSession concept, so a little help would be great.
Thank in advance.
UPDATE
In my UserController, my authentication looks like this:
def authenticate = {
def user = User.findByLoginAndPassword(params.login, params.password)
if(user){
session.user = user
flash.message = "Hello ${user.name}!"
redirect(controller:"event", action:"list")
}else{
flash.message = "Sorry, ${params.login}. Please try again."
redirect(action:"create")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http 会话只不过是在来自单个源(例如浏览器)的一系列请求中维护的数据。
要在会话中添加某些内容,只需执行
session.setAttribute("key", value)
并从会话中获取数据,只需执行
session.getAttribute("key")
Grails 还为会话访问添加了一些奇特的功能,如此处。他们的示例显示
请注意,如果您使用 grails 插件进行身份验证和授权,它可能会提供一种获取用户的方法。例如,Spring Security 为您提供以下
springSecurityService.getCurrentUser()
,具体取决于您的插件的详细信息,该用户可能会或可能不会在会话中。
the http session is nothing more than data that is maintained among a sequence of requests from a single source, like a browser.
To put something on the session, just do
session.setAttribute("key", value)
and to get data off the session just do
session.getAttribute("key")
Grails also adds some fanciness to session access as outlined here. Their example shows
Note that if you are using a grails plugin for authentication and authorization, it will probably provide a way to get the user. For example Spring Security gives you the following
springSecurityService.getCurrentUser()
depending on the details of your plugin, that user might or might not be on the session.
在你的代码中我注意到你写了
当我认为你的意思是
我做的另一件事是使它成为 session.userId 即
session.userId = user.id
然后不确定这是否有必要。
In your code I noticed you wrote
When I think you mean
The other thing I do is make it session.userId ie
session.userId = user.id
and thenNot sure if that's necessary.
您需要使用 .merge():
这是因为当您将实例保存到 HttpSession 时,该实例已与持久化上下文分离。这是来自 grails 的文档:
http://grails.org/doc/2.3.1/ref /Domain%20Classes/merge.html
You'll need to use .merge():
This is because the instance is detached from the persistence context when you save it to HttpSession. Here is the doc from grails:
http://grails.org/doc/2.3.1/ref/Domain%20Classes/merge.html