Play框架:如何路由到公用文件夹中的HTML页面

时间:2014-02-23 17:09:29

标签: playframework routes

以下是我的应用程序布局:

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

我想加载index.html的网址为http://localhost/mymodule/swagger-ui ...以下是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")

上述路线......除了找不到index.html引用的资源(图片,css)。如果我修改这样的路线......

...

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

...然后它按预期工作,并且还加载了引用的资源......但当然我需要提供类似http://localhost/mymodule/swagger-ui/index.html的网址。

有什么建议吗?

3 个答案:

答案 0 :(得分:4)

尝试:

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)

(订单事宜)

答案 1 :(得分:4)

我已经尝试过詹姆斯的建议 - 而且在我看来,这个解决方案更有意义......

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)

...但实际上它只是部分工作,即http://localhost/mymodule/swagger-ui被正确路由到http://localhost/mymodule/swagger-ui/index.html,但随后包含在其中的所有相对路径(例如css/highlight.default.css)被路由到{ {1}}代替http://localhost/mymodule/css/*。也就是说,要使它工作,我必须修改这样的路线:

http://localhost/mymodule/swagger-ui/css/*

上述路线按预期工作

  1. 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) 被路由到http://localhost/mymodule/swagger-ui
  2. http://localhost/mymodule/swagger-ui/index.html根本没有路由,将http://localhost/mymodule/swagger-ui/index.html隐藏到最终用户
  3. index.html中的相对路径将路由到index.html
  4. 我希望有所帮助。

答案 2 :(得分:1)

我更喜欢像这样的重定向

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)

如果您浏览到/ swagger-ui,它将重定向到swagger-ui /,因此下一次调用js,css和images将处于正确的路径