2

目前在我的应用程序中,如果我去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);
4

1 回答 1

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.htmindex.html默认页面应该可以工作。

如果没有,或者由于某种原因您无法更改文件名,您可以简单地扩展 StaticHttpHandler 并覆盖该handle方法以使其执行您想要的操作。

于 2013-04-13T11:02:22.310 回答