使用src / groovy中的会话 [英] Use session inside src/groovy
问题描述
我想在src / groovy类中的函数A()中的会话中设置一个值,并在同一个src / groovy类的函数B()中使用该值。那可能吗?由于它们被覆盖,所以我无法传递函数之间的值。我引用了这个链接,但并不完全理解实现。提前致谢。
编辑:这里函数A是crawler4j的访问函数,而函数B是handlePageStatusCode()
实用程序Java类来处理像src / groovy,src / java或grails-app / services这样的地方访问会话。您可以尝试使用它:
public class SessionUtil {
/ **
*返回当前会话。这可用于会话变量未由Grails设置的类中,例如Services。
* @return the session
* /
public static GrailsHttpSession getSession(){
return WebUtils.retrieveGrailsWebRequest()。getSession();
$ b $ p
$ b 要使用它,无论你需要什么会话,写:
def session = SessionUtil.getSession()
I want to set a value in session inside a function A() in src/groovy class and use that value inside function B() in the same src/groovy class. Is that possible? I cannot pass the value between functions since they are overridden. I referenced this link but did not quite understand the implementation. Thanks in advance.
Edit: Here function A is visit function of crawler4j and function B is handlePageStatusCode()
解决方案 I made a little utility Java class to handle accessing the session in places like src/groovy, src/java or grails-app/services. You could try using it:
public class SessionUtil {
/**
* Returns the current session. This can be used in classes where the session variable is not set by Grails, such as Services.
* @return the session
*/
public static GrailsHttpSession getSession() {
return WebUtils.retrieveGrailsWebRequest().getSession();
}
}
To use it, wherever you need the session you would write:
def session = SessionUtil.getSession()
这篇关于使用src / groovy中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!