8

I m new to grails,just started with a small application,

I'm searching a solution for below problem,

Can any one tell me how can i render to GSP view page of other controller from current controller view page.

With advance Thanks, Laxmi.P

abdul
  • 1,531
  • 1
  • 14
  • 29
Nandita
  • 307
  • 1
  • 6
  • 11
  • see my updated answer, where I have created one link which eventually redirecting to another controller's view page. – Nirmal May 05 '11 at 12:16

3 Answers3

16

You can use either render(view: '/ctrlr/action', model: [fooInstance: foo]) or redirect(controller: 'ctrlr', action: 'action') controller dynamic methods in your action, depending on if you need to user a model you already have, or to completely redirect to that action's logic.

If you're asking about GSP code, there is a render tag.

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
  • according to the grails reference, the controller parameter is not available for the render function. http://grails.org/doc/latest/ref/Controllers/render.html – mjspier May 05 '11 at 11:48
  • Thanks but that is not what i want.I am in my y.gsp page of my second controller. in my y.gsp i am trying to have a link or button which should take me to my x.gsp page of my first controller. – Nandita May 05 '11 at 11:50
  • @shifty: sorry, corrected. @Nandita - then you should update your question to be more precise. – Victor Sergienko May 05 '11 at 11:55
  • ya its not.but how can i achieve my need..is there any other solution – Nandita May 05 '11 at 11:59
  • WHAT is EXACTLY your need? Are you rendering it from an action code, or from GSP? Do you want to provide own model for that view, or just to redirect to the other controller's action? – Victor Sergienko May 05 '11 at 12:01
  • I am trying to render from action code in my second controller to view which has model already.hope u r getting what i trying to say. – Nandita May 05 '11 at 12:07
11

Lets suppose you want to render finalView.gsp of FirstController from normalView.gsp of SecondController having the following structure :

FirstController.groovy
   finalView.gsp
SecondController.groovy
   normalView.gsp

normalView.gsp will have :

<g:link controller="SecondController" action="redirectToFirstController">Redirect to finalView.gsp </g:link>

Then inside your SecondController, define one action called redirectToFirstController

def redirectToFirstController =  {
 redirect(controller:"FirstController",action:"renderFinalView")
}

And inside your FirstController:

def renderFinalView = {
render(view:"finalView");
}
abdul
  • 1,531
  • 1
  • 14
  • 29
Nirmal
  • 4,789
  • 13
  • 72
  • 114
1

not quite sure but I think you have to use ModelAndView Class.

return new ModelAndView("/controller/view", [ model : youModel ])
mjspier
  • 6,386
  • 5
  • 33
  • 43
  • are you looking for a link to another controller ? then you could use the g:link tag. http://grails.org/doc/1.0.x/ref/Tags/link.html – mjspier May 05 '11 at 12:05
  • why do I get a -1 here. ModelAndView class does work. (altough it was not the solution for Nandita) http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.3 Models and Views – mjspier May 09 '11 at 11:38
  • This one worked brilliantly for me. Didn't even know it was there, thanks. – GarethReid Dec 01 '15 at 23:11