Loops are one of the most important and useful part of every programming language. Grails provides various loops in controller and view. Following are the loops in GSP:

  • each: each tag is used to iterate over each element of a list.
    <g:each in="${books}" status="i">
    <p>Index: ${i}</p>
    <p>Title: ${it.title}</p>
    <p>Author: ${it.author}</p>


    • in – The object to iterate over
    • status (optional) – The name of a variable to store the iteration index in.
    • var (optional) – The name of the item. Defaults “it”.
  • while: while is used to executes a condition until the condition returns false.
    <g:while test="${i < 5}">
    <p>Current i = ${i}</p>


    • test – The conditional expression
  • unless: unless is used to renders its body unless all of the specified conditions (test and/or environment) are true.
    <g:unless test="${name == 'fred'}">
    Hello ${name}!
    <g:unless env="production">
    debug: $someDebug


    • test – The expression to test
    • env – An environment name

    Note:- If both are supplied, they are combined using AND.

Hope this helps 🙂 .