Play Framework [2.4.x] - Module route specific name fails with `Assets is not a member of package`

徘徊边缘 提交于 2019-12-13 01:28:04

问题


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

Compilation error obtained:

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. Define Assets and controller classes in their own package

Define Assets class Psmod1Assets.scala as:

package com.escalesoft.psmod1.ctrl

import play.api.http.LazyHttpErrorHandler

object class Psmod1Assets extends controllers.AssetsBuilder(LazyHttpErrorHandler)

The above object definition replacement by class fixes the problem

2. Splitting the route file

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)

If you like you can check or clone the code of my small test project on github:

  • Online: https://github.com/refond/psmod1/tree/routing_issue
  • With git: git clone -b routing_issue https://github.com/refond/psmod1.git

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:

  • 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

回答1:


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)

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

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

My small test project on github mentionned in the problem description is not updated to work with the custom com.escalesoft.psmod1.ctrl.Psmod1Assets



来源:https://stackoverflow.com/questions/33985108/play-framework-2-4-x-module-route-specific-name-fails-with-assets-is-not-a

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