Grails Acegi Plugin - PersonController.groovy - Please Explain!

What does person.properties = params do?

0


a source to share


3 answers


Well, the short answer is that it matches any key in the params map with the properties of the person object, assigning the value in the params map to the corresponding property.

example: let's say params.id = 156 and the person has a member property named id. After this call, person.id will be 156.



Some notes:

  • If the parameters have keys that don't match the properties personally, that's ok, it just won't do anything with those.
  • If there are properties that don't have keys in the parameters? Also good, he will miss them too.
  • This is also very similar to creating a new Person via "new Person (params)" or calling "bindData (person, params)".
+2


a source


There is complete documentation for the Grails website

Behind the scenes, Groovy / Grails object properties are a property map of a domain class. The params object is also a map of the request parameters - basically the CGI parameters of the HttpServletRequest object. Thus, assignment will update the property map with values ​​from the params map, only where they match.

If you were to do this with direct servlets and JSPs, you would essentially be writing:

public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    Person person = new Person();
    person.firstName = request.getParameter('firstname');
    person.lastName =  request.getParameter('lastname');
    person.password = request.getParameter('password');
    ...
}

      



With Grails, you basically just write this in PersonController.groovy:

def save = {
        def person = new Person()
        person.properties = params
        ...
    }

      

So, with Grails, you don't have to worry too much about the parameter names, as you have to use Grails tags to output them and then map the parameters to get them back into the object. This reduces the silly mistakes that occur when using the parameter name incorrectly.

You can also add additional properties to the Person object and not write more getter / setter statements.

+1


a source


It updates the property values ​​of the object person

using the supplied query parameters. This is called data binding and is documented here .

0


a source







All Articles