0
剛剛花了一天的時間試圖讓灰熊靜態內容工作。以下來自Grizzly博客的URL解釋了很多:Grizzly STatic Content 。上下文webapp下面的灰熊靜態內容路徑
我想模仿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());
}
}
}
上面的代碼適用於以下網址:
http://localhost:9090/Sample/SampleAdmin/restmethod
http://localhost:9090/SampleUI/hello.htm
不過,我想靜態頁面,以低於webapp的路徑 「樣本」 像:
http://localhost:9090/Sample/UI/hello.htm
任何幫助將不勝感激。