如何使用Spark Java返回静态html页面? [英] How to return a static html page with Spark Java?

查看:261
本文介绍了如何使用Spark Java返回静态html页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带火花的hello world:

A hello world with spark:

 get(new Route("/hello") {
            @Override
            public Object handle(Request request, Response response) {
                response.type("text/html");
                return "<h1>Hello Spark MVC Framework!</h1>";
            }
        });

如何返回静态文件index.html?

How can I return a static file index.html instead?

注意:


  • 我需要将index.html放在jar中

  • 本着简单的spark java的精神,我想尽可能避免通过模板,这对静态页面来说太过分了。

推荐答案

您可以通过在此方法中将绝对路径传递到静态资源目录来实现此目的:

You can do so by passing the absolute path to your static resources directory in this method:

externalStaticFileLocation("/var/www/public");

或者通过传递此方法中的相对路径:

Or by passing the relative path in this method:

staticFileLocation("/public");

在设置任何路线之前调用此方法。在静态资源目录的根目录中创建index.html文件。

Call this before setting any route. Create your index.html file in the root of your static resources directory.

这篇关于如何使用Spark Java返回静态html页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆