在路径"/post/:postId"中无法加载静态文件在spark-java框架中 [英] Can't load static file while in route "/post/:postId" in spark-java framework
问题描述
在我的spark-java项目中,我将css文件和js文件放入src/main/resources
中的文件夹static
中.我使用Spark.staticFileLocation("/static")
设置静态文件路径.
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.
当我使用get("/posts", (req, res) ->{...})
时,css和js文件可以正确加载.路径为http://0.0.0.0:4567/css/style.css
,效果很好.
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.
但是当我想通过postId get("/post/:postId", (req, res) ->{...})
获取单个帖子时,它无法正确加载css和js文件.路径变为http://0.0.0.0:4567/post/css/style.css
,它不起作用.
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.
如何解决此类问题?
推荐答案
我找到了解决问题的解决方案.
I found the solution to fix it.
我没有使用req.params("postId")
来获取"/post/:postId"
中的参数,而是将路由更改为"http://0.0.0.0:4567/post?postId=1"
,因此我可以使用req.queryParams("postId")
并仍然使用get("/post", (req, res)->{})
来获取参数.
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)->{})
.
在这种情况下,静态文件路径将是正确的.仍然是http://0.0.0.0:4567/css/style.css
.
It this situation, the static file path will be correct. It still be http://0.0.0.0:4567/css/style.css
.
这篇关于在路径"/post/:postId"中无法加载静态文件在spark-java框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!