Sparkjava redirect while keeping the browser URL

被刻印的时光 ゝ 提交于 2019-12-11 04:52:42

问题


I have a sparkjava server app running, it serves a static HTML page using this line:

staticFiles.location("/public");

If you go to http://example.com, you will see the HTML page. Now, I want to redirect users from other paths to my homepage, while keeping the browser URL. For example, if you visit http://example.com/message/123, you will still see the HTML page, while the browser URL stays http://example.com/message/123. So redirect.get() won't work here.


回答1:


In order to serve the same file from different paths, you can do as follows (it looks long but it's pretty simple):

Assuming your project's structure is:

src
  java
    main
      resources
        public
        templates   (optional folder)

On GET request to your homepage a static HTML file that resides in /public is served. Let's call this file index.html.

Now you want to register additional path(s) to serve this file. If you use TemplateEngine you can do it easily. Actually, you'll refer to index.html both as a static file and as a template (with no parameters).

Template engine lets you create the served HTML page dynamically by passing it a map of key-value pairs that you can reference in the template on runtime. But in your case, it will be much simpler because you want to serve a page as-is, statically. Therefore you'll pass an empty map:

Spark.get("/message/123", (req, res) ->
    new ModelAndView(new HashMap(),
                     "../public/index"),
                     new ThymeleafTemplateEngine()
);
  • Thymeleaf is just an example here, Spark supports few template engines. For each one of them, you can find in the documentation a simple github example of how to use it. For example, This is the Thymeleaf one.
  • The path ../public/index is because Spark is looking for templates in the templates folder, and you want to target public/index.html as the template.
  • You'll find the class ThymeleafTemplateEngine in the github link.
  • Of course you'll have to add the chosen template engine dependency to the project's pom.xml file.

As a result, GET requests to both http://example.com and http://example.com/message/123 will serve index.html while keeping the requested URL.




回答2:


You could read the index.html file into a string and serve it. This is what I ended up doing.

If your application runs from compiled .class files:

URL url = getClass().getResource("public/index.html");
String indexDotHTML = new String(Files.readAllBytes(Paths.get(url.toURI())));

get("/message/123", "text/html", (req, res) -> indexDotHTML);

if your application runs from a jar: (solution uses Guava as a helper)

import com.google.common.io.ByteStreams;


InputStream in = getClass().getResourceAsStream("/public/index.html");
String indexDotHTML = new String(ByteStreams.toByteArray(in));

get("/message/123", "text/html", (req, res) -> indexDotHTML);


来源:https://stackoverflow.com/questions/44401973/sparkjava-redirect-while-keeping-the-browser-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!