[英]Grizzly Static Content path below the context webapp
Just spent a day trying to get Grizzly Static Content working. 刚花了一天时间尝试使Grizzly Static Content正常工作。 The following URL from the Grizzly Blog explained alot: Grizzly STatic Content . 以下来自Grizzly Blog的URL进行了很多解释: Grizzly STatic Content 。
I am trying to mimic Tomcat, in that I would the path to the static content to be below the webapp or the context handle. 我试图模仿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());
}
}
}
The above code works with the following URL's: 上面的代码适用于以下URL:
http://localhost:9090/Sample/SampleAdmin/restmethod
http://localhost:9090/SampleUI/hello.htm
However I would like the static pages to be below the webapp path "Sample" Like: 但是我希望静态页面位于webapp路径“ Sample”下方,例如:
http://localhost:9090/Sample/UI/hello.htm
Any help would be appreciated. 任何帮助,将不胜感激。
Well I don't know if I get any badges for answering my own question :) I worked for a couple of days, trying to implement a forward from a filter. 好吧,我不知道是否有任何徽章可以回答我自己的问题:)我工作了几天,试图从过滤器实现转发。 There are multiple unanswered posts on this forum about jersey forwarding. 这个论坛上有许多关于球衣转发的未答复的帖子。 Not obvious how to forward from a filter. 如何从过滤器转发不明显。 EndRant EndRant
My solution was to use the deprecated call to HttpHandlerRegistration.builder() that allowed me to effectively set up a /Sample/UI context. 我的解决方案是使用对HttpHandlerRegistration.builder()的弃用调用,该调用使我可以有效地设置/ Sample / UI上下文。
Now the code behaves like Tomcat where Sample/{SampleAdmin,UI} are the endpoints. 现在,代码的行为类似于Tomcat,其中Sample / {SampleAdmin,UI}是端点。
startServer("http://", "localhost", "Sample", "UI", "9090");
public static void startServer(String protocol, String host, String webapp, String ui, String port) {
try {
String BASE_URI = protocol + host + ":" + port + "/" + webapp + "/";
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));
/*--- Static UI Content ---*/
if(ui!=null && !ui.equals("")) {
String jarPath = getJarPath(SampleGrizzlyWebServer.class);
if (jarPath != null) {
CLStaticHttpHandler clStaticHttpHandler = new CLStaticHttpHandler(
new URLClassLoader(new URL[]{new File(jarPath).toURI().toURL()}),
"/", "/lib/", "/js/", "/css/");
ServerConfiguration sc = httpServer.getServerConfiguration();
sc.addHttpHandler(clStaticHttpHandler, HttpHandlerRegistration.builder().contextPath("/" + webapp + "/" + ui + "").urlPattern("").build());
}
}
/*--- 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);
httpServer.start();
System.out.println("Jersey app started with WADL available at:");
System.out.println(BASE_URI + "SampleAdmin/application.wdl");
System.out.println("Hit enter to stop it...");
System.in.read();
httpServer.shutdown();
}
catch(Exception ex) {
System.err.println("Error: " + ex.getMessage());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.