Java 如何在Grizzly中设置默认索引页
如果我转到Java 如何在Grizzly中设置默认索引页,java,grizzly,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
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);
您没有提到您正在使用的grizzly版本,但从2.2.19开始: 看起来,如果将
index.htm
更改为index.html
默认页面应该可以工作
如果由于某种原因无法更改文件名或无法更改文件名,则只需扩展StaticHttpHandler并重写
handle
方法,使其执行所需操作 目前还没有欢迎页面的概念(至少我记得没有)。我建议记录一个功能请求。如果将index.htm重命名为index.html会发生什么?index.html只在非根级别上对我有效。如果我将StaticHttpHandler映射到root/则StaticHttpHandler甚至不会处理对localhost:8889的请求。你知道为什么吗?(我正在使用灰熊2.2.16和球衣)。我不知道为什么。我确实尝试将我的实现从根目录更改为工作模式,但它不会,因此我正在经历与您相同的事情。但是现在我得换码头了。你有没有得到任何关于这种情况的答案?我也面临同样的问题。