Play Framework [2.4.x]-特定于模块路由的名称失败,原因是“资产不是软件包的成员" [英] Play Framework [2.4.x] - Module route specific name fails with `Assets is not a member of package`
问题描述
我尝试提供模块特定的路由,使用标准路由文件conf/routes
可以将其包含在Play应用程序中,如下所示:
I try to provide module specific routing that can be included in Play applications using the standard route file conf/routes
as:
-> /psmod1 com.escalesoft.psmod1.ctrl.Routes
获得编译错误:
类型 Psmod1Assets 不是软件包的成员 com.escalesoft.psmod1.ctrl
type Psmod1Assets is not a member of package com.escalesoft.psmod1.ctrl
To accomplish this I followed two steps as instructed in the official documentation at Assets and controller classes should be all defined in the controllers.admin package
1.在自己的程序包中定义资产和控制器类
将资产类Psmod1Assets.scala
定义为:
package com.escalesoft.psmod1.ctrl
import play.api.http.LazyHttpErrorHandler
object class Psmod1Assets extends controllers.AssetsBuilder(LazyHttpErrorHandler)
上面的对象定义替换类解决了该问题
2.分割路径文件
将模块特定的路由文件/conf/com.escalesoft.psmod1.ctrl.routes
定义为:
Define module specific route file /conf/com.escalesoft.psmod1.ctrl.routes
as:
# Routes
# Home page
GET / com.escalesoft.psmod1.ctrl.Application.index
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file com.escalesoft.psmod1.ctrl.Psmod1Assets.versioned(path="/public", file: Asset)
如果您愿意,可以在github上检查或克隆我的小型测试项目的代码:
If you like you can check or clone the code of my small test project on github:
- 在线: https://github.com/refond/psmod1/tree/routing_issue
- 使用git:
git clone -b routing_issue https://github.com/refond/psmod1.git
- Online: https://github.com/refond/psmod1/tree/routing_issue
- With git:
git clone -b routing_issue https://github.com/refond/psmod1.git
使用标准controllers.Assets
将项目配置为正常运行.
转到/conf/com.escalesoft.psmod1.ctrl.routes
文件(在上进行检查) com.escalesoft.psmod1.ctrl.routes ),然后将controllers.Assets
行替换为com.escalesoft.psmod1.ctrl.Psmod1Assets
行,以重现编译错误.
The project is configured to work using standard controllers.Assets
.
Go to /conf/com.escalesoft.psmod1.ctrl.routes
file (check it at com.escalesoft.psmod1.ctrl.routes) and replace line with controllers.Assets
by line with com.escalesoft.psmod1.ctrl.Psmod1Assets
to reproduce the compilation error.
我已经检查了以下资源:
I already checked the following resources:
- 看起来像重复的,但已批准的答案无法解决当前问题 http://www.objectify.be/wordpress/?p = 363
- 常规 https://www.playframework.com/documentation/2.4.x/模块
- Looks like a duplicate but the approved answer does not address the current problem Play Framework [2.4.x] how to address public assets in a sub module's routing file
- Interesting but outdated: http://www.objectify.be/wordpress/?p=363
- General https://www.playframework.com/documentation/2.4.x/Modules
推荐答案
资产 class Psmod1Assets.scala定义必须是...一个不是对象的类:
Assets class Psmod1Assets.scala definition must be ... a class not an object:
package com.escalesoft.psmod1.ctrl
import play.api.http.LazyHttpErrorHandler
class Psmod1Assets extends controllers.AssetsBuilder(LazyHttpErrorHandler)
这与Play 2.4建议的InjectedRoutesGenerator设置有关,该设置要求已构建的Assets是一个类,才能从依赖项注入中受益.请参阅 ScalaRouting#Dependency-Injection
This relates to the Play 2.4 recommended InjectedRoutesGenerator setting which requires the built Assets to be a class to benefit from dependency injection. See official documentation at ScalaRouting#Dependency-Injection
请注意,官方文档似乎与此更改并不完全一致,但可能仍声明为object
而不是class
:
https://www.playframework.com/documentation/2.4.x/SBTSubProjects#Assets-and-controller-classes-应该全部定义在controllers.admin程序包中
Be aware that the official documentation seems not to be completely consistent with this change yet an may still state object
instead of class
:
https://www.playframework.com/documentation/2.4.x/SBTSubProjects#Assets-and-controller-classes-should-be-all-defined-in-the-controllers.admin-package
问题描述中提到的我在github上的小型测试项目未更新为可与自定义com.escalesoft.psmod1.ctrl.Psmod1Assets
My small test project on github mentionned in the problem description is not updated to work with the custom com.escalesoft.psmod1.ctrl.Psmod1Assets
这篇关于Play Framework [2.4.x]-特定于模块路由的名称失败,原因是“资产不是软件包的成员"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!