Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails service with different scopes for persistence

I have one domain, in that domain more than 25 members are there. This members value will come from one form. But it feels bad to fill those too much fields. So I thought dividing input form into different stages.

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.

<g:set var="formService" value="${new FormService()}" /> 

class FormService{
    static transactional = false
    static scope = "session"

    FormObject myObject = new FormObject()

    def resetForm(){
        myObject=new FormObject()
    }

    def getForm(){
        return myObject
    }
}

and used that into GSP like

<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?

like image 826
sanghavi7 Avatar asked Feb 01 '13 18:02

sanghavi7


1 Answers

I got the solution....

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:

def action1 = {
   ... regular work
   session.formObject = new FormObject()
   // return model, e.g.
   [foo: bar]
}

and in GSPs you can refer it

${session.formObject}
like image 150
sanghavi7 Avatar answered Oct 09 '22 13:10

sanghavi7