创建会话时在Grails中创建会话变量 [英] Create session variable in Grails when session is created

查看:110
本文介绍了创建会话时在Grails中创建会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何任务执行之前(因为我的所有任务都需要一个init会话范围变量才能运行),我需要在创建会话时准确创建会话范围变量。因为,HttpSession的会话实例是在创建会话时由grails应用程序自动创建的。开发者希望使用会话来存储会话变量,只需使用'会话'实例。我无法找到init会话变量的放置位置。希望有人能帮助我。谢谢

I need to create an session scope variable exactly when a session is created, before any task is execute (because all my tasks need an init session scope variable to run). Because, session instance of HttpSession is automatically create by grails application when a session is created. Developer want to use session for store session variables just use 'session' instance. I can not find out where init session variable will be put. Hope that someone will help me. Thanks

推荐答案

您可以做的是有一个 HttpSessionListener 会话创建和会话删除。

What you can do is have a HttpSessionListener that listens for session creation and session removal.

可以将其从应用程序上下文中更改为:

ou could change it to pull from the application context:

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.codehaus.groovy.grails.commons.ApplicationHolder;

public class MySessionListener implements HttpSessionListener {

private MyListener myListener;

public void sessionCreated(HttpSessionEvent event) {
String id = event.getSession().getId();
getMyListener().sessionCreated(id);
}

public void sessionDestroyed(HttpSessionEvent event) {
String id = event.getSession().getId();
getMyListener().sessionDestroyed(id);
}

public void setMyListener(MyListener myListener) {
this.myListener = myListener;
}

private synchronized MyListener getMyListener() {
if (myListener == null) {
myListener = (MyListener)ApplicationHolder.getApplication().getMainContext().getBean("myListener");
}
return myListener;
}
}

参考这里:,如果这没有

还有其他方法...让我知道!

There is also other method ...let me know!

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

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