java 播放框架:如何更改播放默认包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4816248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:09:13  来源:igfitidea点击:

Play Framework: How to change play default packages?

javaplayframework

提问by Alan Souza

I was just wondering if it is possible to change the default packages from Play. For example: I want to change the "controllers" package to "com.test.controllers". I don't know if this makes any sense, but I just want to know how if it is possible. I did not find anything related to this in play website.

我只是想知道是否可以从 Play 更改默认包。例如:我想将“控制器”包更改为“com.test.controllers”。我不知道这是否有任何意义,但我只想知道如何实现。我在游戏网站上没有找到任何与此相关的内容。

采纳答案by Codemwnci

Updated to make the distinction between play1 and 2 clear.

更新以明确 play1 和 2 之间的区别。

For Play 1.x, this is not possible.

对于 Play 1.x,这是不可能的。

No, all controllers must be in a package, or sub package of controllers. If you wanted to keep a com.testpackage structure, you can do controllers.com.test

不,所有控制器都必须在一个包或控制器的子包中。如果你想保留一个com.test包结构,你可以这样做controllers.com.test

For more info, see this thread.

有关更多信息,请参阅此线程。

For Play2.x, this is possible.Just move everything to the package you desire. Make sure that the outermost app/directory stays at outside. An example would be play-project/app/com.company/controllers.

对于 Play2.x,这是可能的。只需将所有物品移至您想要的包裹即可。确保最外层app/目录位于外部。一个例子是play-project/app/com.company/controllers

So the simple answer is...it depends, on whst version of Play you are using.

所以简单的答案是……这取决于您使用的 Play 版本。

回答by Sam

According to the currentPlay 2.0 documentation, this is now possible:

根据当前的Play 2.0 文档,现在可以这样做:

Note that in Play 2.0, the controllers, models and views package name conventions are now just that and can be changed if needed (such as prefixing everything with com.yourcompany).

请注意,在 Play 2.0 中,控制器、模型和视图包名称约定现在就是这样,可以根据需要进行更改(例如在所有内容前添加 com.yourcompany)。

This works well for an empty Play application, there are however some details to take note of for anything else:

这适用于一个空的 Play 应用程序,但是还有一些细节需要注意:

  1. Importing custom namespaces into view templates will work for any types except for those that are declared in the first line of a template, which are the arguments for the scala renderfunction that is generated out of a view. Our workaround is to add the full package name to type declarations in the first line of view templates.
  2. For every namespace defined in the routes file (e.g. a custom package and the default package for the Assets route), Play 2.0 generates a corresponding routes source file within the same namespace, so you need to take care to address the correct file when e.g. doing redirection.
  1. 将自定义命名空间导入视图模板将适用于任何类型,除了在模板第一行中声明的类型,这些类型是从视图生成的 Scala渲染函数的参数。我们的解决方法是将完整的包名称添加到视图模板第一行的类型声明中。
  2. 对于路由文件中定义的每个命名空间(例如自定义包和资产路由的默认包),Play 2.0 会在相同的命名空间内生成相应的路由源文件,因此在执行时需要注意寻址正确的文件重定向。

回答by dogbane

From the manual:

手册

A Controller class must be defined in the controllerspackage and must be a subclass of play.mvc.Controller.

You can add a Java package before the Controller class name if it isn't defined directly under the controllers package. The controllerspackage itself is implicit, so you don't need to specify it.

控制器类必须在控制器包中定义,并且必须是play.mvc.Controller的子类 。

如果没有直接在控制器包下定义,您可以在控制器类名之前添加一个 Java 包。该控制器包本身是隐含的,所以您不需要指定它。

This means that you can't change your controllerspackage to com.test.controllers(because the root package must be controllers), but you can change to controllers.com.test.

这意味着您不能将您的controllers包更改为com.test.controllers(因为根包必须为controllers),但您可以更改为controllers.com.test.

回答by Rich

There is a discussion of this here.

有这样的讨论在这里

Also there is thisin the FAQ.

常见问题中也有这个