views:

35

answers:

3

Hi,

I am creating a Grails application which has an input page with text fields. Here the user can type in the data and on submit, the control goes to the action in controller. Here I get the value of the form data using params.empName etc.

But the scope of this data is very small and doesnt get carried on if I do a redirect from the current action to another action.

Is there a way to increase the scope of the variables?

I am now to convert this to service oriented architecture. Therefore Is there a way to access these data in the service as well?

Please advice.

Thanks, Megs

A: 

You can add...

params: params

...as an argument to the redirect, so that the incoming params are sent along with the redirect.

wwwclaes
A: 

I don't think there's a built-in way to increase the scope. This is probably a Good Thing.

If you're redirecting in controllers, you should simply pass along the necessary parameters via the redirect() params dynamic property. Example:

def formHandler = {
    // do stuff with params
    redirect(action: 'anotherAction', params: params)
}

If you need scope to span multiple requests, e.g. if you're having a multi-step form entry given to the user, you might look into using web flows to persist state between requests.

For services, you're better off just passing down what you need as arguments to the service method, rather than exposing params. Example (similar to the Accessing Services section here):

// service
def myServiceMethod(def foo, def bar) {
    // do stuff
}

// controller
def myService
def myControllerAction {
    myService.myServiceMethod(params.foo, params.bar)
}

Exposing parameters from the controller to the service layer would break the layer-oriented approach Grails is trying to provide you; i.e. the "model" and "controller" components (of MVC) would be more tightly coupled.

Rob Hruska
Thanks Guys. I l try to implement this.
Megs
1 question here. In this case how does the control go to the view. is it from service...how do we put that down?do we use a redirect to view??PLease advice.Thanks,Megs
Megs
Views are rendered by controller actions. Controller actions can redirect to other controller actions. It sounds like you're somewhat unfamiliar with how Grails works. Perhaps it wouldn't hurt to check out one or more of the tutorials here: http://www.grails.org/Tutorials to gain a better understanding of how Grails works.
Rob Hruska
A: 

I would also take a look at chaining actions as a way to pass the model information

http://www.grails.org/Controllers+-+Redirects

Aaron Saunders
@aaron - You may want to fix your link..
Javid Jamae