Skip to content

grace-guides/gs-spring-boot-gsp

Repository files navigation

Main branch build status Apache 2.0 license Grace on X

Groovy Version Grace Version Spring Boot Version

Spring Boot with GSP

This guide show you how to use GSP as view templates with Spring Boot.

Versions

  • Spring Boot 3.3.7
  • Grace Framework 2023.2.0

Usage

Add dependency grace-plugin-gsp

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'
}

Using GSP and Tags

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.

Running app

./gradlew bootRun
gs-spring-boot-gsp  ./gradlew bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.7)

2025-01-09T15:17:31.025+08:00  INFO 1026 --- [  restartedMain] grace.guides.GraceBootApplication        : Starting GraceBootApplication using Java 17.0.12 with PID 1026 (/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-01-09T15:17:31.027+08:00  INFO 1026 --- [  restartedMain] grace.guides.GraceBootApplication        : No active profile set, falling back to 1 default profile: "default"
2025-01-09T15:17:31.047+08:00  INFO 1026 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-01-09T15:17:31.047+08:00  INFO 1026 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-01-09T15:17:31.506+08:00  INFO 1026 --- [  restartedMain] g.plugins.DefaultGrailsPluginManager     : Total 3 plugins loaded successfully, take in 46 ms
2025-01-09T15:17:31.789+08:00  INFO 1026 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-01-09T15:17:31.796+08:00  INFO 1026 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-01-09T15:17:31.797+08:00  INFO 1026 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.34]
2025-01-09T15:17:31.818+08:00  INFO 1026 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-01-09T15:17:31.818+08:00  INFO 1026 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 771 ms
2025-01-09T15:17:32.206+08:00  INFO 1026 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2025-01-09T15:17:32.213+08:00  INFO 1026 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoints beneath base path '/actuator'
2025-01-09T15:17:32.332+08:00  INFO 1026 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-01-09T15:17:32.362+08:00  INFO 1026 --- [  restartedMain] grace.guides.GraceBootApplication        : Started GraceBootApplication in 1.483 seconds (process running for 1.842)
2025-01-09T15:17:32.371+08:00 DEBUG 1026 --- [  restartedMain] PluginsInfoApplicationContextInitializer :
----------------------------------------------------------------------------------------------
Order      Plugin Name                              Plugin Version                     Enabled
----------------------------------------------------------------------------------------------
    1      Core                                     2023.2.0                                 Y
    2      Codecs                                   2023.2.0                                 Y
    3      GroovyPages                              2023.2.0                                 Y
----------------------------------------------------------------------------------------------

then open your browser, http://localhost:8080/greeting You will see it works!

Links

About

Spring Boot with GSP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published