This guide show you how to use GSP as view templates with Spring Boot.
- Spring Boot 3.3.11
- Grace Framework 2023.3.0-RC2
- Groovy 4.0.26
Adding grace-plugin-gsp
plugin to the build.gradle
,
dependencies {
implementation 'org.graceframework:grace-boot'
implementation 'org.graceframework:grace-plugin-core'
implementation 'org.graceframework:grace-plugin-gsp'
}
In the following example, GreetingController
is a Spring Controller, handles GET requests for /greeting
by returning the name of a View (in this case, greeting/index
).
A View
is responsible for rendering the HTML content. The implementation of the method body relies on a view technology (in this case, GSP
) to perform server-side rendering of the HTML.
@Controller
class GreetingController {
@GetMapping("/greeting")
String index(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name)
return "greeting/index"
}
}
Groovy Servers Pages (or GSP
for short) is Grails' view technology. It is designed to be familiar for users of technologies such as ASP and JSP, but to be far more flexible and intuitive.
The following listing (from app/views/greeting/index.gsp
) shows the index.gsp
template:
<!DOCTYPE HTML>
<html>
<head>
<title>Spring Boot with GSP</title>
<meta name="layout" content="main"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1><g:welcome/></h1>
<p>Hello, <b>${name}</b> from GSP!</p>
</body>
</html>
All built-in GSP tags start with the prefix g:
. Unlike JSP, you don’t specify any tag library imports. If a tag starts with g:
it is automatically assumed to be a GSP tag. In this example (from app/taglib/grace/guides/GreetingTagLib.groovy
) GreetingTagLib
tag would look like:
class GreetingTagLib {
GrailsApplication grailsApplication
def welcome = { args, body ->
out << "Welcome to Grace " << grailsApplication.metadata.getGrailsVersion()
}
}
A tag library is a simple Groovy class that ends with the convention TagLib
and place it within the app/taglib
directory.
./gradlew bootRun
gs-spring-boot-gsp ./gradlew bootRun
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.11)
2025-05-13T23:43:46.934+08:00 INFO 29193 --- [ restartedMain] grace.guides.GraceBootApplication : Starting GraceBootApplication using Java 17.0.15 with PID 29193 (/Users/rain/Development/github/grace/grace-guides/gs-spring-boot-gsp/build/classes/groovy/main started by rain in /Users/rain/Development/github/grace/grace-guides/gs-spring-boot-gsp)
2025-05-13T23:43:46.935+08:00 INFO 29193 --- [ restartedMain] grace.guides.GraceBootApplication : No active profile set, falling back to 1 default profile: "default"
2025-05-13T23:43:46.953+08:00 INFO 29193 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-05-13T23:43:46.954+08:00 INFO 29193 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-05-13T23:43:47.373+08:00 INFO 29193 --- [ restartedMain] g.plugins.DefaultGrailsPluginManager : Total 3 plugins loaded successfully, take in 42 ms
2025-05-13T23:43:47.601+08:00 INFO 29193 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-05-13T23:43:47.607+08:00 INFO 29193 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-05-13T23:43:47.607+08:00 INFO 29193 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.40]
2025-05-13T23:43:47.629+08:00 INFO 29193 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-05-13T23:43:47.629+08:00 INFO 29193 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 675 ms
2025-05-13T23:43:47.980+08:00 INFO 29193 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-05-13T23:43:47.987+08:00 INFO 29193 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 15 endpoints beneath base path '/actuator'
2025-05-13T23:43:48.098+08:00 INFO 29193 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-05-13T23:43:48.126+08:00 INFO 29193 --- [ restartedMain] grace.guides.GraceBootApplication : Started GraceBootApplication in 1.323 seconds (process running for 1.664)
2025-05-13T23:43:48.135+08:00 DEBUG 29193 --- [ restartedMain] PluginsInfoApplicationContextInitializer :
----------------------------------------------------------------------------------------------------------
Order Plugin Name Plugin Version Enabled
----------------------------------------------------------------------------------------------------------
1 Core 2023.3.0-RC2 Y
2 Codecs 2023.3.0-RC2 Y
3 GroovyPages 2023.3.0-RC2 Y
----------------------------------------------------------------------------------------------------------
then open your browser, http://localhost:8080/greeting
You will see it works!