问题
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