0

I've seen various answers to similar questions, but not for this specific case:

I have a Grails gsp with several fields and a submit button. I want to enter a value in the first field, click submit, and have the controller return values into the remaining fields (including the original entry field). I want to do this with a simple page refresh (no AJAX) and it does not involve a database. I have it working fine with two gsp pages that are identical but have different names. I just call the first one, enter the value, and have the submit action point to the second. But I would like to be able to do it with a single gsp. A simplified version of my controller looks like this:

class CalculatorController {

    def index = {
    redirect(action: "input")
    }

    def input= {}

    def calculateValues = {

        def inputString = params.enterValue
        def value2 = 'You entered something on the page'
        [field1: inputString, field2: value2]
   }
}
4
  • What is the problem that you are having? Can you just check in calculateValues if params.enterValue exists? Jun 10, 2012 at 22:27
  • If my only view is input.gsp and the submit button calls calculateValues, it gives me the error that calculateValues.gsp does not exist. If I put a redirect(action: "input") inside of the calculateValues action, the view renders with null values in all of the fields. Jun 10, 2012 at 23:26
  • 1
    you need to have all of logic inside of the same closure if you want to use only one gsp. Test if the params are passed in and return the correct vals appropriately Jun 10, 2012 at 23:32
  • @user1435174: if that works, please click the "accept V mark" right on the left of chrislovecnm's answer (below the number). That's how our community do to show appreciation for help :) Jun 12, 2012 at 3:25

2 Answers 2

0

you need to have all of logic inside of the same closure if you want to use only one gsp. Test if the params are passed in and return the correct vals

0

Actually you don't need to have all of your logic in the same closure. Simply specify the view template to use in calculateValues...

def calculateValues = {
  def inputString = params.enterValue
  def value2 = 'You entered something on the page'
  render(view: 'input', model: [field1: inputString, field2: value2])
}

By using the render method you can have multiple controller action reuse the same gsp view.

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.