(Quick Reference)

6.4.8 Applying Constraints - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.4.8 Applying Constraints

URL Mappings also support Grails' unified validation constraints mechanism, which lets you further "constrain" how a URL is matched. For example, if we revisit the blog sample code from earlier, the mapping currently looks like this:
Los mapeos de URL tambien soportan el mecanismo unificado de Grails validation constraints, el cual permite ademas "restringir" como una URL es empatada. Por ejemplo: si volvemos al codigo de ejemplo del blog, el mapeo actualmente se ve asi:

static mappings = {
   "/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show")
}

This allows URLs such as:
Esto permite URLs tales como:

/graemerocher/2007/01/10/my_funky_blog_entry

However, it would also allow:
Sin embargo, esto permitiria tambien:

/graemerocher/not_a_year/not_a_month/not_a_day/my_funky_blog_entry

This is problematic as it forces you to do some clever parsing in the controller code. Luckily, URL Mappings can be constrained to further validate the URL tokens:
Esto es problematico por que lo obliga a hacer algun parseo inteligente en el codigo del controlador. Afortunadamente, los mapeos de URL pueden ser restringidos para ademas validar los tokens de la URL:

"/$blog/$year?/$month?/$day?/$id?" {
     controller = "blog"
     action = "show"
     constraints {
          year(matches:/\d{4}/)
          month(matches:/\d{2}/)
          day(matches:/\d{2}/)
     }
}

In this case the constraints ensure that the year, month and day parameters match a particular valid pattern thus relieving you of that burden later on.
En este caso las restricciones se aseguran de que los parametros year, month y day empaten con un patron particular valido asi relevandolo a usted de esa carga despues.