如何在Play中组织java和scala代码?

时间:2017-02-13 13:23:37

标签: java scala playframework typesafe-activator

activator new

结果:

Fetching the latest list of templates...



Browse the list of templates: http://lightbend.com/activator/templates
Choose from these featured templates or enter a template name:
  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala
(hit tab to see a list of all templates)
>

play-javaplay-scala的解剖结构如下:

ls project-*/app
...-java/app:
controllers  filters  Filters.java  Module.java  services  views

...-scala/app:
controllers  filters  Filters.scala  Module.scala  services  views

根据this文件:

  

您还可以选择使用SBT使用的默认布局   Maven的。请注意,此布局是实验性的,可能有   的问题。要使用此布局,必须禁用布局   插件并设置旋转模板的显式监控:

build.sbt                  → Application build script
src                        → Application sources
 └ main                    → Compiled asset sources
    └ java                 → Java sources
       └ controllers       → Java controllers
       └ models            → Java business layer
    └ scala                → Scala sources
       └ controllers       → Scala controllers
       └ models            → Scala business layer
    └ resources            → Configurations files and other non-compiled resources (on classpath)
       └ application.conf  → Main configuration file
       └ routes            → Routes definition

讨论

基于this answer,应该结合java和scala文件吗? E.g:

合并

...-java/app/controllers:
AsyncController.java  CountController.java  HomeController.java

...-scala/app/controllers:
AsyncController.scala  CountController.scala  HomeController.scala

.../app/controllers
AsyncController.java  CountController.java  HomeController.java
AsyncController.scala  CountController.scala  HomeController.scala

这是用于组合java和scala类的Play解剖,还是应该在app / controllers文件夹中创建java和scala目录?

scala和java项目中的routes文件是相同的:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# An example controller showing how to use dependency injection
GET     /count                      controllers.CountController.count
# An example controller showing how to write asynchronous code
GET     /message                    controllers.AsyncController.message

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

2 个答案:

答案 0 :(得分:3)

只需将controllersmodels文件夹放入app文件夹,然后将scala和java文件放入其中。

build.sbt            → Application build script
app                  → Application sources
 └ controllers       → controllers (java or scala)
 └ models            → business layer (java or scala)
 └ views             
public
conf
 └ application.conf  → Main configuration file
 └ routes            → Routes definition

答案 1 :(得分:1)

您不需要特殊的“组合”布局 - 您可以在相同的包中使用java和scala。

示例:

package controllers;

public class MyJavaClass {

  public static String getName(){
    return "My Name";
  }
}

enter image description here