gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:23:36 26 4
gpt4 key购买 nike

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

最佳答案

你没有提到你使用的是什么版本的灰熊,但是从 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 方法以使其执行您想要的操作。

关于java - 如何在 Grizzly 中设置默认索引页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15976015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com