简体   繁体   中英

How to serve JSPs using Guice 3.0?

I'm trying to serve a JSP from Guice. I don't find any basic examples on how to do this!

My setup :

web.xml

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.example.Bootstrap</listener-class>
</listener>

org.example.Bootstrap (something like...)

public class Bootstrap extends GuiceServletContextListener 
{
    @Override
    protected Injector getInjector()
    {
        return Guice.createInjector(new org.example.BootstrapModule());
    }

}

org.example.BootstrapModule (something like...)

public class BootstrapModule extends ServletModule
{
    @Override
    protected void configureServlets()
    {
        // serve .JSPs
        bind(org.apache.jasper.servlet.JspServlet.class).in(Scopes.SINGLETON);
        serveRegex("/.*\\.jsp").with(org.apache.jasper.servlet.JspServlet.class);

        // serve my controllers 
        bind(MainServlet.class).in(Scopes.SINGLETON);
        serveRegex("/.*").with(MainServlet.class);
    }
}

In MainServlet , I do something like :

request.getRequestDispatcher("test.jsp").include(request, response);

or

request.getRequestDispatcher("test.jsp").forward(request, response);

or

request.getRequestDispatcher("/test.jsp").include(request, response);

or

request.getRequestDispatcher("/test.jsp").forward(request, response);

My test.jsp is in webapp/test.jsp (I use Maven).

It doesn't work! I always end up with errors like :

SEVERE: PWC6117: File XXX not found

It seems the informations Guice passes to org.apache.jasper.servlet.JspServlet are not the ones required for the JSPs to work.

What am I missing? Do I even have to specify org.apache.jasper.servlet.JspServlet manually? What is required to correctly serve JSPs from Guice?

It seems this is a known bug .

As a workaround, some say you can compile the TRUNK of Guice. I also found that setting

request.setAttribute(org.apache.jasper.Constants.JSP_FILE, "/test.jsp");

before the forwarding also works.

But I have to run more tests to see what I'll use until Guice is fixed in a public release.

You need to override Bootstrap#getModule() to return a new BootstrapModule() .

@Override
protected Module getModule() {
    return new BootstrapModule();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM