Freemarker configuration in Dropwizard

1,966 views
Skip to first unread message

Rob

unread,
Jun 9, 2014, 6:41:12 PM6/9/14
to dropwiz...@googlegroups.com
Just want to confirm I'm not missing something: From looking at the source, it seems that the only way to access the Freemarker configuration object would be to implement my own version of FreemarkerViewRenderer so I can make freemarker.template.Configuration accessible. Is that correct, or is there a better way to accomplish this?

The use case is that I want to set a Freemarker shared variable to make it convenient to use some custom Freemarker tags (e.g. config.setSharedVariable("custom", new CustomTags())).



jonathan mukiibi

unread,
Jun 11, 2014, 4:05:09 AM6/11/14
to dropwiz...@googlegroups.com
Am new to Freemaker and Dropwizard but I have created a view but each time I  make a request to the specified path I get io.dropwizard.views.ViewRenderException: Unable to find a renderer for /com/example/drop/views/screen.ftl and yet everything is fine when i critically look at the documentation.


On Mon, Jun 9, 2014 at 3:41 PM, Rob <r...@uberlogik.com> wrote:
Just want to confirm I'm not missing something: From looking at the source, it seems that the only way to access the Freemarker configuration object would be to implement my own version of FreemarkerViewRenderer so I can make freemarker.template.Configuration accessible. Is that correct, or is there a better way to accomplish this?

The use case is that I want to set a Freemarker shared variable to make it convenient to use some custom Freemarker tags (e.g. config.setSharedVariable("custom", new CustomTags())).



--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick Telford

unread,
Jun 11, 2014, 7:29:09 AM6/11/14
to dropwiz...@googlegroups.com
Jonathan, are you using the Maven Shade plugin to build a fat jar? If so, ensure you have the services transformer configured for it: http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ServicesResourceTransformer

jonathan mukiibi

unread,
Jun 12, 2014, 3:58:42 AM6/12/14
to dropwiz...@googlegroups.com
I have it already but i still get the exception. But may be you can help me look at what i could be doing wrong..!!

MY view Class

package com.example.drop.views;

import io.dropwizard.views.View;

public class ScreenView extends View {
    private final Screen screen;

    public ScreenView(Screen screen) {
        super("screen.ftl");
        this.screen = screen;
    }

    public Screen getScreen() {
        return screen;
    }
}

My Resource Method
 for the view
@Path("/{id}")
    @Produces(MediaType.TEXT_HTML)
    @GET
    @Timed
    @ExceptionMetered
    public ScreenView getScreen(@PathParam("id") String id) {
        return new ScreenView(screenDao.findById(id));
    }
My ftl Template
<#-- @ftlvariable name="" type=" com.example.drop.views.ScreenView"-->
<html>
<body>
<!-- calls getScreen().getLocation() and sanitizes it -->
<div>${screen.location}</div>
<div>${screen.targetArea}</div>
</body>
</html>

And this is also in a resource folder in  package com.example.drop.views

But still when i make a request to the specified path with an id  I get io.dropwizard.views.ViewRenderException: Unable to find a renderer for /com/example/drop/views/screen.ftl

i have the following in my run method of the application

final ScreenDao screenDao= jdbi.onDemand(ScreenDao.class);
environment.jersey().register(new ScreenResource(screenDao));

And i also have the following in the initialize method:
bootstrap.addBundle(new ViewBundle());


--

Nick Telford

unread,
Jun 12, 2014, 7:31:16 AM6/12/14
to dropwiz...@googlegroups.com
Try this:

bootstrap.addBundle(new ViewBundle(ImmutableList.of(new FreemarkerViewRenderer())));

If that works, then it's a service discovery issue.

Nick Telford

unread,
Jun 12, 2014, 7:32:45 AM6/12/14
to dropwiz...@googlegroups.com
Also, can you confirm that you have both dropwizard-views and dropwizard-views-freemarker as dependencies in the POM of your project.

jonathan mukiibi

unread,
Jun 14, 2014, 8:33:01 AM6/14/14
to dropwiz...@googlegroups.com
Thanks Nick. It now works fine but is there a way i can use views to add items to my database like the way i use them to get stuff from the database into the Freemarker template.I would love to use it other than creating a separate web application to interact with my Api.

Thanks in advance..!!


David ‘Bombe’ Roden

unread,
Sep 15, 2015, 2:05:09 PM9/15/15
to dropwiz...@googlegroups.com
On 2014-06-09 22:41:12 +0000, Rob said:

> Just want to confirm I'm not missing something: From looking at the
> source, it seems that the only way to access the Freemarker
> configuration object would be to implement my own version of
> FreemarkerViewRenderer so I can make freemarker.template.Configuration
> accessible. Is that correct, or is there a better way to accomplish
> this?

I have had success with subclassing ViewBundle, overriding
getViewConfiguration() to return a map from “.ftl” (the view renderer’s
suffix) to another map containing the options I wanted to set in
Freemarker. In my case I set the “object_wrapper” property to
“my.custom.ObjectWrapper(2.3.21)” and Freemarker happily created and
used my custom object wrapper.


Greetings,

Bombe


Reply all
Reply to author
Forward
0 new messages