Grails中的httpSession [英] httpSession in Grails

查看:131
本文介绍了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: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: )
user.save()
render(template:/ artist / rate,model:[artist:artist,rating:rating])
}
}

我仍然在努力完全理解httpSession的概念,所以有一点帮助会很棒。
在此先感谢您。


UPDATE

在我的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 =对不起,$ {params.login}。请重试。
redirect(action:create)
}
}


解决方案

http会话仅仅是来自单一来源(如浏览器)的一系列请求之间维护的数据。



要在会话中添加一些内容,请执行

session.setAttribute(关键,价值)



并从会话中获取数据只需做

session.getAttribute(key)



Grails还增加了会话访问的一些功能,如这里。他们的例子显示:

  def user = session [user] 
session [user] =John
assetJohn== session.user

请注意,如果您使用的是grails插件进行身份验证和授权,它可能会提供一种获取用户的方法。例如,Spring Security为您提供以下内容:
$ b $ p $ spring $ securityCurrentUser()



取决于您的插件的详细信息,该用户可能会或可能不在会话中。


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")
  }
}

解决方案

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

def user = session["user"]
session["user"] = "John"
asset "John" == session.user

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.

这篇关于Grails中的httpSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆