【问题标题】:play2 calling controllers, models, views in submoduleplay2 在子模块中调用控制器、模型、视图
【发布时间】:2026-01-21 14:20:04
【问题描述】:

试图将我的项目拆分为几个子模块。 我创建了子模块: /模块/公共 /模块/购物

现在我正在尝试将名称空间转换为包括新结构在内的新名称空间。

我的控制器:

package controllers.common;
public class Index extends Controller {}

我的模特:

package models.common;
@Entity
public class AppMode {}

我的看法:

@(AppModeForm: Form[models.common.AppMode], CurrentMode: Boolean)
 @helper.form(common.routes.CMS.appModeSubmit, 'id -> "form") {}

而且我总是遇到错误。 F.e: 视图中的错误:

reference to common is ambiguous; it is imported twice in the same scope by import controllers._ and import models._


[error] /home/kd/Application/modules/common/app/views/CMS/AppModeView.scala.html:9: reference to common is ambiguous;
[error] it is imported twice in the same scope by
[error] import controllers._
[error] and import models._
[error]                     @helper.form(common.routes.CMS.appModeSubmit, 'id -> "form") {
[error]                                  ^
[error] one error found

【问题讨论】:

    标签: java namespaces routes playframework-2.0 sbt


    【解决方案1】:

    modelscontrollers 会自动导入到播放模板中,因此这会导致问题,您要么不必将它们命名为相同,(例如将包结构转为 common.{models, controllers},然后显式从那里导入你想要的东西。当然,由于可能有不止一个叫做路由的东西,你仍然会遇到一些重载问题(但你可以通过重命名为当前范围导入的东西来解决这个问题:

    @import controllers.common.{routes => commonRoutes}
    
    @helper.form(commonRoutes.CMS.appModeSubmit, 'id -> "form") 
    

    一个更简单的选择是明确指定包,如下所示:

    @helper.form(controllers.common.routes.CMS.appModeSubmit, 'id -> "form") 
    

    【讨论】:

    • 它只在当前子模块(普通)内有效,但我如何从其他子模块(例如购物)访问路线?