Class RendererSupport<T>

  • Type Parameters:
    T - The type of object this renderer renders
    All Implemented Interfaces:
    Renderer<T>
    Direct Known Subclasses:
    GroovyRendererSupport

    public abstract class RendererSupport<T>
    extends java.lang.Object
    implements Renderer<T>
    A Renderer super class that provides a getType() implementation based on the generic type of the impl.

    Implementations need only to declare the type they render as the value for type variable T and implement render(ratpack.handling.Context, Object).

    
     import ratpack.handling.Context;
     import ratpack.render.RendererSupport;
     import ratpack.test.embed.EmbeddedApp;
    
     import static org.junit.Assert.assertEquals;
    
     public class Example {
    
       // A type of thing to be rendered
       static class Thing {
         private final String name;
    
         public Thing(String name) {
           this.name = name;
         }
    
         public String getName() {
           return this.name;
         }
       }
    
       // Renderer implementation
       public static class ThingRenderer extends RendererSupport<Thing> {
         public void render(Context context, Thing thing) {
           context.render("Thing: " + thing.getName());
         }
       }
    
       public static void main(String... args) throws Exception {
         EmbeddedApp.fromHandlers(c -> c
           .register(r -> r.add(new ThingRenderer()))
           .all(ctx -> ctx.render(new Thing("foo")))
         ).test(httpClient -> {
           assertEquals("Thing: foo", httpClient.getText());
         });
       }
     }
     

    An alternative to implementing a render is to make the type to be rendered implement Renderable.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected RendererSupport()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Class<T> getType()
      The type of object that this renderer can render (the type for T).
      abstract void render​(Context ctx, T t)
      Render the given object to the response.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RendererSupport

        protected RendererSupport()
    • Method Detail

      • getType

        public java.lang.Class<T> getType()
        The type of object that this renderer can render (the type for T).
        Specified by:
        getType in interface Renderer<T>
        Returns:
        The type of object that this renderer can render.
      • render

        public abstract void render​(Context ctx,
                                    T t)
                             throws java.lang.Exception
        Render the given object to the response.

        Calling this method will finalize the processing, sending the response to the client.

        Any errors that occur during rendering will be sent to Context.error(Throwable).

        Specified by:
        render in interface Renderer<T>
        Parameters:
        ctx - the context for the operation
        t - the object to render
        Throws:
        java.lang.Exception - if anything goes wrong while rendering