Play Framework [2.4.x] 如何在子模块的路由文件中处理公共资产 [英] Play Framework [2.4.x] how to address public assets in a sub module's routing file

查看:15
本文介绍了Play Framework [2.4.x] 如何在子模块的路由文件中处理公共资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于当前讨论的目的,上一个问题可以作为子模块"含义的基准:Play Framework [2.4.x] 使用子模块

This previous question may serve as a baseline for what "submodules" mean for the purpose of the current discussion: Play Framework [2.4.x] Working with Sub Modules

如果您了解 Play 子模块,那么在给定上下文的情况下,如何输入子模块上的路由条目以从公共"文件夹中公开资产?

If you understand a Play submodule then given that context how does one enter the routing entry on the submodule to expose assets from a "public" folder?

每当我尝试创建基本条目(如下所示)时,我的整个根模块都无法编译单个路由,但我没有收到可用"错误或其他指示可能发生的情况.

Whenever I attempt to make a basic entry (as follows) my entire root module fails to compile a single route and yet I get no "usable" error or other indicator as to what might have happened.

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

即使我注释掉根项目中的条目,也会发生编译器错误.

The compiler errors occur even if I comment out the entry in the root project.

因为我所有的子模块控制器都有一个前缀,我试过(当然)

As all my sub modules controllers have a prefix I tried that (of course)

GET   /assets/*file  controllers.submodule.Assets.at(path="/public", file)

唉,这也行不通,但至少我得到一个有用的错误,告诉我 类型资产不是包控制器的成员.submodule

Alas that doesn't work either but at least I get a useful error telling me the type Assets is not a member of package controllers.submodule

有什么建议吗?

PS:是的,我也尝试从根的路由文件中取出条目,以防它只是名称空间冲突...

PS: Yes I've also tried taking the entry out of the root's routing file in case it was just a name space collision...

推荐答案

你必须在你的子模块控制器包中创建一个 Assets 控制器:

You have to create an Assets controller in your submodule controllers package:

package controllers.submodule

class Assets extends AssetsBuilder(DefaultHttpErrorHandler)

然后在子模块的路由器文件(应该命名为 submodule.routes)中为这个控制器声明一个路由:

Then declare a route for this controller in the router file (it should be named submodule.routes) of your submodule:

GET    /submodule/public/*file    controllers.submodule.Assets.versioned(path="/public", file: Asset)

最好在路由前加上你的子模块的名称以避免路由冲突,以防你有其他带有静态路由条目的子模块名称.

It is better to prefix the route with the name of your submodule to avoid route colision in case you have other submodules name with static route entry.

还有一件事:所有静态文件都托管在根模块的lib"文件夹中:

One more thing: all your static files are hosted in the "lib" folder of your root module:

lib/submodule/css...
lib/submodule/js...

所以你必须像这样更新你的视图:

So you have to update your views like that:

<script type="text/javascript" src="@controllers.submodule.routes.Assets.versioned("lib/submodule/js/your-submodule-script.js")"></script>

不要忘记将其添加到根模块的路由文件(命名路由)中

don't forget to add this to your route file (named routes) of your root module

-> / submodule.routes

这篇关于Play Framework [2.4.x] 如何在子模块的路由文件中处理公共资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆