如何响应 HTML 页面 SparkJava
How to response with HTML page SparkJava
我找到了 and 个答案,但其中 none 个有效。第一个仅适用于 index.html
(您不需要指定路径和类似的东西)。尽管文件存在并且 Spark returns index.html
.
,但第二个解决方案的代码给了我 NullPointerException
帮手class
class Helper(){
String renderContent(String htmlFile) {
try {
return new String(Files.readAllBytes(Paths.get(getClass().getResource(htmlFile).toURI())), StandardCharsets.UTF_8);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
mailSendingList.add(e.toString());
}
return null;
}
}
路线
get("/404", (req, res) -> helper.renderContent("404.html"));
异常
java.lang.NullPointerException
at Helper.renderContent(Helper.java:177)
at Main.lambda$main(Main.java:33)
at spark.SparkBase.handle(SparkBase.java:311)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:159)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:60)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:451)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:527)
at java.lang.Thread.run(Thread.java:745)
检查getClass().getResource(htmlFile)
的return,这可能是空的,因为没有找到资源。
如果您有文件“404.html”和类路径的根目录,请将您的代码更改为(注意插入“/”)
get("/404", (req, res) -> helper.renderContent("/404.html"));
我的文件路径错误。将 ("404.html")
更改为 ("public/404.html")
有帮助。
我找到了 index.html
(您不需要指定路径和类似的东西)。尽管文件存在并且 Spark returns index.html
.
NullPointerException
帮手class
class Helper(){
String renderContent(String htmlFile) {
try {
return new String(Files.readAllBytes(Paths.get(getClass().getResource(htmlFile).toURI())), StandardCharsets.UTF_8);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
mailSendingList.add(e.toString());
}
return null;
}
}
路线
get("/404", (req, res) -> helper.renderContent("404.html"));
异常
java.lang.NullPointerException
at Helper.renderContent(Helper.java:177)
at Main.lambda$main(Main.java:33)
at spark.SparkBase.handle(SparkBase.java:311)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:159)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:60)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:451)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:527)
at java.lang.Thread.run(Thread.java:745)
检查getClass().getResource(htmlFile)
的return,这可能是空的,因为没有找到资源。
如果您有文件“404.html”和类路径的根目录,请将您的代码更改为(注意插入“/”)
get("/404", (req, res) -> helper.renderContent("/404.html"));
我的文件路径错误。将 ("404.html")
更改为 ("public/404.html")
有帮助。