[xwiki-users] Using session inside a class
On Mar 19, 2009, at 10:16 AM, Jan Cerny wrote:
Hi, I'm trying to access session from a Groovy class, but I'm not successful.
When I have a simple script:
<% session = request.getSession(true);
counter = session.getValue("counter") %>
- everything is OK.
But when I have a class:
class User {
def counter = 0;
void setUser() {
session = request.getSession(true); counter = session.getValue("counter") }
String showCounter() { return this.counter; } }
The Error message is No such property: request for class: User.
So how should I access session from the class?
You need to pass the request to your groovy class. See for example http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyClassHelloWorldTutor...
Thanks -Vincent
Hi, thanks for a quick response. Somehow I managed it. I thought I tried this way yesterday, but apparently I made a mistake somwhere. Thanks, Zik
Hi, I have another problem. I want to use a class that extends a parent class and then use parseGroovyFromPage. But I can't work out how to do it. An example here: --------------------------------------------------------------------- page ParentObject: <% class ParentObject { def counter = 0; def session; void setSession(session) { this.session = session; } } %> --------------------------------------------------------------------- page UserClass: /* Groovy Class #* */ public class User extends ParentObject { void setUser() { if (session.getValue("counter") != null) this.counter = session.getValue("counter") + 1; else this.counter = 1; session.setAttribute("counter", this.counter); } String showCounter() { return this.counter; } } /* *# */ --------------------------------------------------------------------- script page: #set ($groovyObject = $xwiki.parseGroovyFromPage("Include.UserClass")) --------------------------------------------------------------------- The question is how to include the ParentObject page into UserClass page, so that UserClass can extend the ParentObject. Any help would be appreciated. Thanks, Zik
Hi I think you'll need in your User class to use $xwiki.parseGroovyFromPage("ParentObject") Before that you need to pass $xwiki to User class. public class User extends ParentObject { private xwiki = null; void setUser() { if (session.getValue("counter") != null) this.counter = session.getValue("counter") + 1; else this.counter = 1; session.setAttribute("counter", this.counter); } String showCounter() { return this.counter; } public void setXwiki(xwiki) { this.xwiki = xwiki; } } -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Jan Cerny Sent: 24 March 2009 08:43 To: [email protected] Subject: [xwiki-users] Using parseGroovyFromPage on inherited class Hi, I have another problem. I want to use a class that extends a parent class and then use parseGroovyFromPage. But I can't work out how to do it. An example here: --------------------------------------------------------------------- page ParentObject: <% class ParentObject { def counter = 0; def session; void setSession(session) { this.session = session; } } %> --------------------------------------------------------------------- page UserClass: /* Groovy Class #* */ public class User extends ParentObject { void setUser() { if (session.getValue("counter") != null) this.counter = session.getValue("counter") + 1; else this.counter = 1; session.setAttribute("counter", this.counter); } String showCounter() { return this.counter; } } /* *# */ --------------------------------------------------------------------- script page: #set ($groovyObject = $xwiki.parseGroovyFromPage("Include.UserClass")) --------------------------------------------------------------------- The question is how to include the ParentObject page into UserClass page, so that UserClass can extend the ParentObject. Any help would be appreciated. Thanks, Zik _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users -------------------------------------------------------- NOTICE This message and any files transmitted with it is intended for the addressee only and may contain information that is confidential or privileged. Unauthorised use is strictly prohibited. If you are not the addressee, you should not read, copy, disclose or otherwise use this message, except for the purpose of delivery to the addressee. Any views or opinions expressed within this e-mail are those of the author and do not necessarily represent those of Coventry University.
Hi, that doesn't work. xwiki.parseGroovyFromPage("ParentObject") returns an object of the class. I only need to include the class, so that UserClass can extend it. Hi I think you'll need in your User class to use $xwiki.parseGroovyFromPage("ParentObject") Before that you need to pass $xwiki to User class. public class User extends ParentObject { private xwiki = null; void setUser() { if (session.getValue("counter") != null) this.counter = session.getValue("counter") + 1; else this.counter = 1; session.setAttribute("counter", this.counter); } String showCounter() { return this.counter; } public void setXwiki(xwiki) { this.xwiki = xwiki; } } -- View this message in context: http://n2.nabble.com/Using-session-inside-a-class-tp2502340p2525971.html Sent from the XWiki- Users mailing list archive at Nabble.com.
Please, anyone knows? Is it possible at all? I need to know... -- View this message in context: http://n2.nabble.com/Using-session-inside-a-class-tp2502340p2531499.html Sent from the XWiki- Users mailing list archive at Nabble.com.
participants (3)
-
Ajdin Brandic -
Jan Cerny -
Zik23