Play Framework: How to route to a HTML page in the public folder

房东的猫 提交于 2019-12-04 19:21:30

Try:

GET /swagger-ui         controllers.Assets.at(path = "/public/swagger-ui", file = "index.html")
GET /swagger-ui/*file   controllers.Assets.at(path = "/public/swagger-ui", file)

(Order matters)

I've tried what James suggested – and in my opinion was the solution that made more sense...

GET     /swagger-ui         controllers.apidocs.Assets.at(path = "/public/swagger-ui", file = "index.html")
GET     /swagger-ui/*file   controllers.apidocs.Assets.at(path = "/public/swagger-ui", file)

... but actually it only worked partially, i.e. http://localhost/mymodule/swagger-ui was correctly routed to http://localhost/mymodule/swagger-ui/index.html, but then all the relative paths contained in it (e.g. css/highlight.default.css) were routed to http://localhost/mymodule/css/* instead of to http://localhost/mymodule/swagger-ui/css/*. That said, to make it work I had to modify the routes like this:

GET     /swagger-ui         controllers.apidocs.Assets.at(path = "/public/swagger-ui", file = "index.html")
GET     /*file              controllers.apidocs.Assets.at(path = "/public/swagger-ui", file)

The routes above work as expected

  1. http://localhost/mymodule/swagger-ui is routed to http://localhost/mymodule/swagger-ui/index.html
  2. http://localhost/mymodule/swagger-ui/index.html is not routed at all, hiding index.html to end users
  3. Relative paths in index.html are routed to http://localhost/mymodule/swagger-ui/css/*

I hope that helps.

I prefer a redirect first like this

GET     /swagger-ui          controllers.Default.redirect(to = "swagger-ui/")
GET     /swagger-ui/         controllers.apidocs.Assets.at(path = "/public/swagger-ui", file = "index.html")
GET     /swagger-ui/*file              controllers.apidocs.Assets.at(path = "/public/swagger-ui", file)

If you browse to /swagger-ui it will redirect to swagger-ui/ so the next calls to js, css and images will be in the correct path

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