4

我正在努力让 Guice Servlet 配置 Jetty 如何为静态页面提供 Web 请求,在这个简单的情况下。

我创建了一个简单的应用程序,它应该映射两个不同的请求,一个使用 GuiceServlet,另一个不使用。后者有效,而 GuiceServlet 映射的则返回 404 错误。

有小费吗?我正在使用:JDK 1.7.0_15;eclipse.jetty.jetty-servlet 8.1.9.v20130131;guice-servlet 3.0。谢谢。

public class Main {
    public static void main(String... args) {
        Guice.createInjector().getInstance(Main.class).start();
    }

    public void start() {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addEventListener(new MyGuiceServletConfig());
        handler.addServlet(MyServlet.class, "/non-guice");
        server.setHandler(handler);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
}

public class MyGuiceServletConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule() {
            @Override
            protected void configureServlets() {
                System.out.println("MyGSC->getInjector->configureServlets"); //I'm seeing this in the console...
                serve("/guice").with(MyServlet.class);
            }
        });
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

除此之外,创建各种注射器的最佳方法是什么?我的 main(..) 结构是这样的,这样我就可以插入其他模块,让 MyServletModule 在 MyGuiceServletConfig 中指定,正如我在某处看到的那样 - 这是正确的吗?

4

2 回答 2

3

我最终能够以一种有效的方式简单得多。需要为“/”路径添加 DefaultServlet:

public class MyMain {
    public static void main(String... args) throws Exception {
        Guice.createInjector(new MyServletModule());
        Server server = new Server(8080);    
        ServletContextHandler handler = 
            new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addFilter(GuiceFilter.class, "/*", allOf(DispatcherType.class));
        handler.addServlet(DefaultServlet.class, "/");
        server.start();
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

public class MyServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/guice").with(MyServlet.class);
    }
}
于 2013-03-05T21:13:20.293 回答
1

如果您希望 Jetty 提供静态内容,请确保您也配置了 DefaultServlet。

在 Jetty 嵌入式示例树中找到的示例:OneServletContext.java

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class OneServletContext
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        // Serve static content from /tmp
        ServletHolder holder = context.addServlet(DefaultServlet.class,"/tmp/*");
        holder.setInitParameter("resourceBase","/tmp");
        holder.setInitParameter("pathInfoOnly","true");

        // Serve some hello world servlets
        context.addServlet(new ServletHolder(new HelloServlet()),"/*");
        context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
        context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");

        server.start();
        server.join();
    }
}

这会将文件系统目录/tmp中的内容作为http://localhost:8080/tmp/.

例子:

File System       URL
/tmp/hello.txt    http://localhost:8080/tmp/hello.txt
/tmp/a/hi.txt     http://localhost:8080/tmp/a/hi.txt
/tmp/index.html   http://localhost:8080/tmp/
于 2013-03-05T19:28:52.883 回答