digitaljoel
digitaljoel

Reputation: 26574

Guice and JSF 2

I'm trying to use Guice to inject properties of a JSF managed bean. This is all running on Google App Engine (which may or may not be important)

I've followed the instructions here:

http://code.google.com/docreader/#p=google-guice&s=google-guice&t=GoogleAppEngine

One problem is in the first step. I can't subclass the Servlet module and setup my servlet mappings there because Faces is handled by the javax.faces.webapp.FacesServlet which subclasses Servlet, not HttpServlet. So, I tried leaving my servlet configuration in the web.xml file and simply instantiating a new ServletModel() along with my business module when creating the injector in the context listener described in the second step.

Having done all that, along with the web.xml configuration, my managed bean isn't getting any properties injected. The method is as follows

@ManagedBean
@ViewScoped
public class ViewTables implements Serializable
{
    private DataService<Table> service;

    @Inject
    public void setService( DataService<Table> service )
    {
        this.service = service;
    }
    public List<Table> getTables()
    {
        return service.getAll();
    }
}

So, I'm wondering if there is a trick to get Guice injecting into a JSF managed bean? I obviously can't use the constructor injection because JSF needs a no-arg constructor to create the bean.

Upvotes: 2

Views: 3477

Answers (6)

SimonT
SimonT

Reputation: 165

As information in this post are getting out of date but the question is still relevant, I'd like to share my findings about this topic. I wrote a little tutorial including a runnable sample project on how to setup a fully guice powered web stack. You can find it here: https://github.com/skuzzle/guice-jsf

Upvotes: 0

user401810
user401810

Reputation: 11

How about this approach, works well for us:

http://uudashr.blogspot.com/2008/12/guicing-jsf-with-guice.html

Upvotes: 1

jerry
jerry

Reputation: 1

check out http://code.google.com/p/guice2jsf/, and website starchu.blogspot.com, it has excellent library that provides Guice and JSF 2.0 integration

Upvotes: 0

user266230
user266230

Reputation:

being the developer of jsf sugar I really would like to know the problem you had using it. We are already using it in production here so there shouldn't be any "show stoppers", maybe something is just not well documented? Just drop me a mail: murbanek(at)gmx_net (replace the _ with a .) .

Upvotes: 0

Dhanji R Prasanna
Dhanji R Prasanna

Reputation: 39

You can also create an HTTP servlet that then simple delegates the request on to a FacesServlet (like a wrapper). This should give you the same effect using Guice Servlet.

Upvotes: 1

Related Questions