Can't load static file while in route “/post/:

2019-09-11 05:05发布

In my spark-java project, I put css file and js file into folder static in src/main/resources. I use Spark.staticFileLocation("/static") to set the static file path.

When I use get("/posts", (req, res) ->{...}), css and js file can load correctly. The path is http://0.0.0.0:4567/css/style.css, it works well.

But when I want to get the single post by postId get("/post/:postId", (req, res) ->{...}), it can't load the css and js file correctly. The path become http://0.0.0.0:4567/post/css/style.css, it can't work.

How to fix such these problem?

1条回答
家丑人穷心不美
2楼-- · 2019-09-11 05:41

I found the solution to fix it.

Instead of using req.params("postId") to get the params in "/post/:postId", I change the route to "http://0.0.0.0:4567/post?postId=1", so I can get the params by using req.queryParams("postId"), and still using the get("/post", (req, res)->{}).

It this situation, the static file path will be correct. It still be http://0.0.0.0:4567/css/style.css.

查看更多
登录 后发表回答