GSP Views and Layouts

Site Admin · 11 Sep 2026 · 7 views

What is GSP?

Groovy Server Pages (GSP) is Grails's view technology for generating HTML. GSP files live in grails-app/views and use a mix of HTML and Groovy expressions to render dynamic content.

Your First GSP

<!-- grails-app/views/book/list.gsp -->
<%@ page contentType="text/html" %>
<html>
<head><title>Book List</title></head>
<body>
    <h1>All Books</h1>
    <g:each in="${books}" var="book">
        <p>${book.title} by ${book.author}</p>
    </g:each>
</body>
</html>

GSP Tags

Grails provides a rich set of GSP tags for common operations:

  • <g:each> - Iterates over collections
  • <g:if> - Conditional rendering
  • <g:link> - Creates links using URL mappings
  • <g:form> - Renders forms with CSRF protection
  • <g:render> - Includes other GSP templates

Layouts with Sitemesh

Grails uses Sitemesh for layout decoration. Layout files live in grails-app/views/layouts:

<!-- grails-app/views/layouts/main.gsp -->
<html>
<head>
    <title><g:layoutTitle/></title>
    <asset:stylesheet src="application.css"/>
</head>
<body>
    <div class="container">
        <g:layoutBody/>
    </div>
    <asset:javascript src="application.js"/>
</body>
</html>

Key Points

  • GSP is Grails's server-side view technology using HTML and Groovy.
  • GSP tags handle iteration, conditionals, links, and forms.
  • Layouts use Sitemesh to wrap pages with a common structure.
  • <g:layoutTitle/> and <g:layoutBody/> inject page content into layouts.
  • Assets are managed through the asset pipeline.
Share this post:

Comments (0)

Please login or register to comment.