Can't load static file while in route “/post/:postId” in spark-java framework

走远了吗. 提交于 2019-12-12 03:29:43

问题


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:


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.



来源:https://stackoverflow.com/questions/37031480/cant-load-static-file-while-in-route-post-postid-in-spark-java-framework

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