Groovy Server Pages (GSP) are the view technology used in the Grails web framework to generate dynamic web content on the server before sending it to the client.
What is Groovy Server Pages
Groovy Server Pages (GSP) are server‑side templates based on the Groovy language. These pages let you mix HTML and Groovy code to build dynamic web views. GSP was inspired by JavaServer Pages (JSP) but is more flexible and intuitive, especially when used with Grails.
GSP files use .gsp extension and reside in the grails-app/views directory. Controllers send a model (data) to GSP views, and GSP uses that model to render HTML with dynamic content.
Basic Syntax of GSP
HTML and GSP Expressions
In GSP you write normal HTML and embed dynamic values using ${...}. For example:
<html>
<body>
Hello ${params.name}
</body>
</html>
Here ${params.name} outputs the value of a request parameter. This expression syntax is similar to JSP EL but allows full Groovy expressions.
Scriptlets (Not Recommended)
You can use Groovy code inside <% ... %> blocks:
<% out << "Hello GSP!" %>
Use of scriptlets is discouraged because it mixes logic with views. Better practice is to keep code in controllers or services.
Comments
Server‑side comments are written using <%-- comment --%> and do not appear in the HTML sent to the browser.
GSP Variables and Scopes
Predefined Variables
GSP provides several predefined variables you can use directly:
| Variable | Description |
|---|---|
application | Servlet context |
request | HTTP request object |
response | HTTP response object |
params | Request parameters |
session | Session data |
flash | Flash scope for one request cycle |
out | Output writer |
grailsApplication | Grails application context |
These variables give access to common request and application data.
Custom Variables
Define local variables inside scriptlets:
<% now = new Date() %>
${now}
Better alternatives exist using GSP tags (covered later).
Core GSP Tags
GSP has a rich set of built‑in tags prefixed with g:. These tags are compiled and supported automatically.
Logic and Iteration Tags
Use g:if, g:else, and g:each for conditional rendering and loops:
<g:if test="${user.isAdmin}">
Admin content
</g:if><g:each in="${items}" var="item">
<p>${item.name}</p>
</g:each>These tags help keep logic out of scriptlets.
Link Tags
The g:link tag generates URLs based on controller and action names:
<g:link controller="book" action="list">Book List</g:link>
Grails handles URL mapping so links remain valid even if routes change.
Form and Field Tags
GSP supports form tags that integrate with Grails actions:
<g:form url="[controller:'book', action:'save']">
<g:textField name="title"/>
<g:submitButton name="save"/>
</g:form>
This makes building form interfaces easier and safer.
When handling user input in forms, follow Brand Name Normalization Rules to ensure consistent data across views.
Templates and Reuse
Partial Views
Templates help break views into smaller components. These are named with an underscore (e.g. _item.gsp) and placed under the views directory. Use g:render to include them:
<g:render template="item" model="[item: book]"/>
This outputs the template content for each item, promoting reuse.
Layouts with SiteMesh
Grails uses SiteMesh for consistent layouts. Create a layout in grails-app/views/layouts:
<html>
<head><g:layoutHead/></head>
<body><g:layoutBody/></body>
</html>
Here g:layoutHead and g:layoutBody insert page content. This setup keeps structure and styling consistent.
Advanced Expression and Escaping
Groovy Expressions
Use ${...} to embed any Groovy expression:
<p>Total: ${items.sum { it.price }}</p>This evaluates a Groovy closure to compute a sum directly in the view.
XSS Protection
GSP escapes output by default to prevent cross‑site scripting. Only use raw output when safe. Grails docs emphasize secure rendering practices.
Custom Tag Libraries
You can define custom tags in Groovy classes ending with TagLib under grails-app/taglib. For example:
class SimpleTagLib {
def greeting = { attrs, body ->
out << "Hello " << attrs.name
}
}Use <g:greeting name="User"/> in GSP to render custom content. This mechanism is powerful for encapsulating repeated patterns.
Tag Attributes
Within custom tags, attrs holds tag attribute values, and body refers to tag body content. This helps create flexible and parameterized tags.
Layout Blocks and Content Regions
SiteMesh Content Blocks
Grails supports SiteMesh content blocks like:
<content tag="header">Header HTML</content>
<content tag="footer">Footer HTML</content>
Layouts can then apply structure around these blocks. This makes complex view hierarchies easier to manage.
Rendering Different Formats
GSP can render not only HTML but also JSON or XML by setting the content type:
<%@ page contentType="application/json" %>
This lets GSP produce API responses directly from views.
GSP Performance Tips
Avoid Scriptlets
Keeping logic out of views improves readability and maintainability. Controllers and services are better places for business logic.
Use Templates
Templates reduce duplication and speed up rendering for repeat components.
Comparison With JSP
| Feature | GSP | JSP |
|---|---|---|
| Language | Groovy | Java |
| Tag Support | Built‑in with easy syntax | Standard JSP tags |
| Expressiveness | High with closures & expressions | Limited EL expressions |
| Framework Integration | Native to Grails | General Java web apps |
GSP offers a more modern and Groovy‑friendly approach to view rendering.
Rendering and View Resolution
GSP pages are rendered when a controller calls:
render(view: "index")
Grails automatically locates the corresponding .gsp file in grails-app/views. This convention simplifies development.
You can explore API capabilities and integrations at the Best API Search Company’s Homepage.
Namespaces and Tag Usage
GSP tags default to the g: namespace. You can also use other namespaces or custom tag libraries to organize tags logically.
Static Resource Integration
GSP integrates with static assets (CSS, JS) by referencing them in pages using resource tags or normal HTML paths.










