上下文webapp下的java Grizzly静态内容路径
只是花了一天时间试图让Grizzly静态内容正常工作。Grizzly博客的以下URL解释了很多:Grizzly STatic Content
我试图模仿Tomcat,因为我希望静态内容的路径位于webapp或上下文句柄之下
public class SampleAdminApplication extends ResourceConfig {
public SampleAdminApplication() {
packages("com.companyname.sample.sampleadmin.server.services");
}
}
public class SampleGrizzlyWebServer {
public static void main(String[] args) throws IOException {
try {
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
URI.create("http://localhost:9090/Sample/"));
/*--- Static Content ---*/
String jarPath = getJarPath(SampleGrizzlyWebServer.class);
CLStaticHttpHandler clStaticHttpHandler = new CLStaticHttpHandler(
new URLClassLoader(new URL[] {new File(jarPath).toURI().toURL()}),
"/", "/lib/", "/js/", "/css/");
ServerConfiguration sc = httpServer.getServerConfiguration();
sc.addHttpHandler(clStaticHttpHandler,"/SampleUI");
/*--- SampleAdmin WebappContext ---*/
WebappContext SampleAdminContext = new WebappContext("WebappContext", "/" + webapp + "/" + "SampleAdmin");
/*--- Servlet ---*/
final ResourceConfig sampleAdminRc = new SampleAdminApplication();
ServletRegistration sampleAdminRegistration = SampleAdminContext.addServlet("ServletContainer", new ServletContainer(sampleAdminRc));
sampleAdminRegistration.addMapping("/*");
/**
* Deploy Server
*/
SampleAdminContext.deploy(httpServer);
/**
* Start Server
*/
httpServer.start();
} catch (Exception ex) {
System.err.println("Error: " + ex.getMessage());
}
}
}
以上代码适用于以下URL:
http://localhost:9090/Sample/SampleAdmin/restmethod
http://localhost:9090/SampleUI/hello.htm
不过,我希望静态页面位于webapp路径“示例”下方 比如:
http://localhost:9090/Sample/UI/hello.htm
任何帮助都将不胜感激
# 1 楼答案
我不知道我是否因为回答自己的问题而获得任何徽章:) 我工作了几天,试图实现过滤器的转发。这个论坛上有很多关于泽西转发的帖子没有回复。不清楚如何从过滤器中转发。忍耐
我的解决方案是使用不推荐使用的对HttpHandlerRegistration的调用。builder()使我能够有效地设置/Sample/UI上下文
现在,代码的行为类似于Tomcat,其中Sample/{samplemin,UI}是端点