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

2019-04-15 16:59发布

问题:

Here below is my application layout:

myapp
  + app
  + conf
  + modules
  |   + mymodule
  |       + app
  |       + conf
  |       + public
  |           + swagger-ui
  |                + css
  |                + images
  |                + index.html
  + public
  + ...

I want to load index.html with url http://localhost/mymodule/swagger-ui... and here below are my routes in modules/mymodule/conf/mymodule.routes:

...

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file       controllers.mymodule.Assets.at(path = "/public", file)
GET /swagger-ui         controllers.mymodule.Assets.at(path = "/public/swagger-ui", file = "index.html")

The routes above work... except that the resources (images, css) referenced by index.html are not found. If I modify the routes like this...

...

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

... then it works as expected and referenced resources are also loaded... but of course I need to provide an url like http://localhost/mymodule/swagger-ui/index.html.

Any suggestion?

回答1:

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)



回答2:

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.



回答3:

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