1

This may be a basic question but I am new to grails and tried looking for a solution. I need to have a global variable in my groovy controller, a list for example, which is updated in one of the closures (def) and its value is updated in that def, this new value for the list should be available in other closure/def. I tried simply:

class MyController {
  def myList = []

  def def1 = { myList = someList.clone();}

  def def2 = { println myList}
}

The value of myList should be displayed as the updated value from def1, right now it stays empty. Would static help? or is there a method to pass variables between closures directly, since I dont want the huge list to be passed to GSP and back in the second def.

1
  • for this use case you can simply use the session. store the myList variable into the user's session.
    – hitty5
    Nov 2, 2011 at 9:00

2 Answers 2

4

Your controllers should stay stateless. Controllers are instantiated at each action request, so your member variable myList will be thrown away between actions. If you define it static, it will be shared by all instances of MyController.

If you define myList static, all clients accessing your controller will get the same result. If that's what you want, your list should probably be stored through domain objects in your database (see chapter 5 of Grails documentation, Object Relational Mapping).

If you want each client to have their own myList shared between controller actions, what you are looking for are called web flows. They provide a mechanism to store state between Controller actions performed by a given client. Web flows are documented in Grails documentation (Chapter 6 - The Web Layer).

0
0

Of course I perfectly agree with @antoine about stateless of controller. Broadly speaking, MVC paradigm change very much the way to consider variables in programming: you have always to consider GORM and DB if you want to persist values.
Anyway, there could be an interesting alternative way to deal with global values; gloabal values for all controller actions and also for services: using user session. For example:

Class MyController {
   def myService

   def act1 = {
       session.myvalues = [:]
       session.myvalues.myflag = true
       ...
       myService.service1()
   }
}

and in a service using it

Class MyService {
   def service1() {
       def sess = RequestContextHolder.currentRequestAttributes().getSession()
       if (sess?.myvalues?.myflag) println "Found true in myflag"
   }
}

So, I prepare in user session an empty map named "myvalues" and inside it I put a flag. Then I get back the session in the service and I found inside myflag correctly updated. Keep in mind that this globality is restricted to user session with all limitations about user session. But that is correct because a user session is something global until the user is using the system, kept separated from all other users using it at the same time. And losing the values if the same user get another session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.