Chapter 6. Serving Web Assets

Although much of the conversation and example code in this book thus far has focused on building applications that send simple responses back to clients, Ratpack also has an extensive and flexible infrastructure for serving and generating web content. In addition to being able to serve static content from within a project, Ratpack also supports the ability to generate dynamic content on a per-request basis. This chapter will walk you through working with Ratpack’s content serving and generation mechanisms to build full-featured web applications.

Web content, including static assets and templates, can be served from a directory within the project, a JAR on the classpath, or from a directory on the filesystem. To accommodate serving content, Ratpack’s ServerConfig must be supplied with a base directory through the baseDir property. With a Ratpack Groovy project, the base directory will be automatically discovered based on the location of the Ratpack.groovy file (src/ratpack unless explicitly configured otherwise). For main class Ratpack applications, the application base directory can also be automatically discovered by placing a .ratpack marker file within your application’s project structure.

Serving Static Content

Before you begin serving static content from within your application, you should create a subdirectory within the base directory of your project from which static content will be derived. The naming of this directory is arbitrary to its ...

Get Learning Ratpack now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.