Announcement Announcement Module
Collapse
No announcement yet.
Question about Grails Service property persistence in different scopes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question about Grails Service property persistence in different scopes

    Hello, I'm writing a simple page that takes a bunch of input from a user and generates some XML from their responses.

    The input is gathered by a simple HTML form, however there is quite a bit of data and I decided to split it into 3 or 4 pages.

    I've made a class called FormObject that has fields for ALL the input needed.
    The problem is theres no way to pass this object between pages.

    I was thinking maybe a service with the scope of session would allow me to keep a reference to a FormObject and just call a method from the service to get it again.

    IE:

    Code:
    <g:set var="formService" value="${new FormService()}" />  //(after page importing it of course)
    The service looks like this:

    Code:
    class FormService{
        static transactional = false
        static scope = "session"
    
        FormObject myObject = new FormObject()
    
        def resetForm(){
            myObject=new FormObject()
        }
    
        def getForm(){
            return myObject
        }
    }
    And the call in the page is like this:

    Code:
    <g:set var="myForm" value="${formService.getForm()}" />
    However the data doesn't persist between pages.

    It does persist if I define the myObject property as static, but I'm worried that when this hits production, the myObject will be shared across all users.

    Can anyone confirm what would happen if I made it static?
    Would each session have a static form object or would there only be one static form object?

  • #2
    Well I just figured out my answer, having it static will still persist it across all sessions.

    Can anyone suggest a good way of doing this? I don't want it as a domain object since it's not going to be saved, the form is filled out once and it sends results to a different app ( a webservice)

    Comment


    • #3
      The first approach is close, except that you're creating a new instance instead of getting it as a Spring bean, so the fact that it's session-scoped isn't coming into play. In general if you have workflows that span more than two pages, you should look at WebFlow, but that might be a bigger solution than you need.

      I would skip the service wrapper and just store the object in the session. The risk here is that you can end up polluting your sessions with objects if something happens and you don't remove them, so you should handle that.

      In the first controller action make sure the object is there:

      Code:
      def action1 = {
         ... regular work
         session.formObject = new FormObject()
         // return model, e.g.
         [foo: bar]
      }
      and then in your GSPs you can refer to it easily with

      Code:
      ${session.formObject}

      Comment

      Working...
      X