Search code examples
grailscontrollergsp

How can I pass data between GSP and controller in Grails without storing in the database?


Is it possible to enter data in the GSP view and use this data in the controller inside the program to do some operations without storing this data in the domain. For example I have a g:textField and I enter my name. I want to be able to use the name that I enter in the controller to manipulate.


Solution

  • None of the data passed from a view to a controller has to line up with any particular Domain. There are a couple of ways you could do this.

    the view:

    <g:textField name="name" />
    

    the controller:

    class SomeController {
      def someAction() {
        def name = params.name
        // do something with name
      }
    }
    

    You could also use a Command Object.

    the command object:

    @Validateable
    class SomeCommand {
      String name
    
      static constraints = {
         name nullable: false
      }
    }
    

    the controller:

    class SomeController {
      def someAction(SomeCommand someCommand) {
        if (!someCommand.hasErrors()) {
          def name = someCommand.name
          // do something with name
        }
      }
    }