如何在Grizzly中设置默认索引页面

时间:2013-04-12 16:04:32

标签: java grizzly

目前在我的应用程序中,如果我转到http://localhost:8181/index.htm它的效果很好,如果我转到http://localhost:8181我会收到404错误。如何告诉Grizzly默认加载index.htm页面?

    final HttpServer server = HttpServer.createSimpleServer(".", 8181);

    WebappContext ctx = new WebappContext("Socket", "/");

    //enable annotation configuration
    ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
    ctx.addContextInitParameter("contextConfigLocation", "com.production");

    //allow spring to do all of it's stuff
    ctx.addListener("org.springframework.web.context.ContextLoaderListener");

    //enable web socket support
    final WebSocketAddOn addon = new WebSocketAddOn();
    for (NetworkListener listener : server.getListeners()) {
        listener.registerAddOn(addon);

        //if false, local files (html, etc.) can be modified without restarting the server
        listener.getFileCache().setEnabled(false);
    }

    //add jersey servlet support
    ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new ServletContainer());
    jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
    jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    jerseyServletRegistration.setLoadOnStartup(1);
    jerseyServletRegistration.addMapping("/api/*");

    //add atmosphere servlet support
    AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
    AtmosphereFramework f = atmosphereServlet.framework();

    ReflectorServletProcessor r = new ReflectorServletProcessor();
    r.setServletClassName("com.sun.jersey.spi.spring.container.servlet.SpringServlet");

    f.addAtmosphereHandler("/socket/*", r);

    ServletRegistration atmosphereServletRegistration = ctx.addServlet("AtmosphereServlet", atmosphereServlet);
    atmosphereServletRegistration.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json");
    atmosphereServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
    atmosphereServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    //atmosphereServletRegistration.addMapping("/socket/*");
    atmosphereServletRegistration.setLoadOnStartup(1);

    //serve static assets
    StaticHttpHandler staticHttpHandler = new StaticHttpHandler("src/main/web");
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

    //deploy
    logger.info("Deploying server...");
    ctx.deploy(server);

1 个答案:

答案 0 :(得分:2)

您没有提到您正在使用的灰熊版本,但截至2.2.19:

http://grepcode.com/file/repo1.maven.org/maven2/org.glassfish.grizzly/grizzly-http-server/2.2.19/org/glassfish/grizzly/http/server/StaticHttpHandler.java

看起来如果您将index.htm更改为index.html,默认页面应该可以正常工作。

如果没有,或者由于某种原因你无法更改文件名,你可以简单地扩展StaticHttpHandler并覆盖handle方法,使其达到你想要的效果。