Integration with guice ( and guice-servlet )

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Integration with guice ( and guice-servlet )

ggratacap
This post was updated on .
Hello everybody,

I played a little with guice and thymeleaf, and I created a very little module to bridge the two. Nothing is spectacular, lots of things are hardcoded, and I'm not very experimented with guice ( only worked with gin, the GWT version ).

Anyway you can get the code from github :

https://github.com/ggratacap/thymeleaf-guice-test

Here is the getInjector implementation for configuring the application :

        protected Injector getInjector() {
                return Guice.createInjector(new ServletModule(){
                        @Override
                        protected void configureServlets() {
                                serve("*.html").with(ThymeleafServlet.class);
                        }
                }, new ThymeleafModule(){
                        @Override
                        protected void configureThymeleaf() {
                                resolveWith(ServletContextTemplateResolver.class)
                                        .using(TemplateMode.XHTML)
                                        .prefix("/WEB-INF/templates/")
                                        .suffix(".html")
                                        .cacheTTLms(3600000L);
                        serve("/index.html").usingTemplate("index").with(IndexController.class,"handleIndex");
                        }
                });
        }

Edit : I dunno why, but when I commited the project, the part of the web.xml about guice wasn't saved. This has been corrected.
Reply | Threaded
Open this post in threaded view
|

Re: Integration with guice ( and guice-servlet )

danielfernandez
Administrator
Hi!

I'm no Guice expert, but I've had a look at your code and it looks quite interesting. Are you planning to write some kind of tutorial around this?

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Integration with guice ( and guice-servlet )

ggratacap
I think that before writing tutorials I need to play a little more with it, there are things I quite don't like in my code.

I also went a little further than only doing thymeleaf bindings with the ability to map an url to a class specific method and a template ( if the parameters are correct ). This is heavily inspired by Spring-web's RequestMapping, and I don't think this should be directly linked to thymeleaf bindings.

I also need to do a bit of research on Guice-Servlet as I didn't look deep inside...

Geraud
Reply | Threaded
Open this post in threaded view
|

Re: Integration with guice ( and guice-servlet )

ggratacap
Note that I deleted the previous repository.

New one ( cleaner ) : https://github.com/ggratacap/thymeleaf-guice
Reply | Threaded
Open this post in threaded view
|

Re: Integration with guice ( and guice-servlet )

ggratacap
In reply to this post by ggratacap
I'm actually thinking to change a bit the "register" method. Actually it is like this :

    register("/index.html").usingTemplate("index").with(IndexController.class,"handleIndex");

However I think it would be better to start with the controller class and do things like this :

    // Register specific method
    register(IndexController.class).method("handleIndex").with("/index.html").usingTemplate("index");
    // Register all methods of the controller with specific annotation
    register(AnnotatedController.class).byAnnotation();
    // Register all methods of the controller with specific annotation excepted 'AnnotatedController#doNotHandleThis'
    register(AnnotatedController.class).byAnnotation().except("doNotHandleThis");

If anybody has suggestions ...