user1729603
user1729603

Reputation: 83

Why does my static route change in spark framework when using get request?

I have the following simple main class that executes Spark.

    Spark.port(4570);       
    final Configuration configuration = new Configuration(new Version(2, 3, 0));
    configuration.setClassForTemplateLoading(SparkHandler.class, "/");

    Spark.staticFileLocation("/public");

    Spark.get("/", (request, response) -> {

        // read patterns

        // attributes for web-interface.
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("data", "someData");

        return new ModelAndView(attributes, "timeline.ftl");
    } , new FreeMarkerEngine());

Everything woks fine. When I go to http://localhost:4570/ I got the requested web-page!

I now change the path in the get statement to /a/b/c but execute the very same code:

    Spark.port(4570);   
    final Configuration configuration = new Configuration(new Version(2, 3, 0));
    configuration.setClassForTemplateLoading(SparkHandler.class, "/");

    Spark.staticFileLocation("/public");

    Spark.get("/a/b/c", (request, response) -> {

        // read patterns

        // attributes for web-interface.
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("data", "someData");

        return new ModelAndView(attributes, "timeline.ftl");
    } , new FreeMarkerEngine());

If I now go to e.g. http://localhost:4570/a/b/c, it returns messages that lots of resources that could previously be found are not available any more. E.g.

INFO 28/07/16 14:45:03:The requested route [/a/b/vis/vis.js] has not been mapped in Spark

However, it is exactly in the location /public/vis/vis.js.

Does that get command change my static directory? Or is something happening here that I just do not understand :).

Upvotes: 0

Views: 64

Answers (1)

user1729603
user1729603

Reputation: 83

I found the answer!

In my freemarker/html file I used relative parts like e.g.

<script src="./vis/vis.js"></script>

Changing them to absolute paths solves the problem:

<script src="/vis/vis.js"></script>

Sorry for the silly question, but maybe it helps others.

Upvotes: 1

Related Questions