play的目录结构制作的相当精简,以下是从play官网截下的图片:

      

app                      → Application sources
 └ assets                → Compiled asset sources
    └ stylesheets        → Typically LESS CSS sources
    └ javascripts        → Typically CoffeeScript sources
 └ controllers           → Application controllers
 └ models                → Application business layer
 └ views                 → Templates
build.sbt                → Application build script
conf                     → Configurations files and other non-compiled resources (on classpath)
 └ application.conf      → Main configuration file
 └ routes                → Routes definition
dist                     → Arbitrary files to be included in your projects distribution
public                   → Public assets
 └ stylesheets           → CSS files
 └ javascripts           → Javascript files
 └ images                → Image files
project                  → sbt configuration files
 └ build.properties      → Marker for sbt project
 └ plugins.sbt           → sbt plugins including the declaration for Play itself
lib                      → Unmanaged libraries dependencies
logs                     → Logs folder
 └ application.log       → Default log file
target                   → Generated stuff
 └ resolution-cache      → Info about dependencies
 └ scala-2.10
    └ api                → Generated API docs
    └ classes            → Compiled class files
    └ routes             → Sources generated from routes
    └ twirl              → Sources generated from templates
 └ universal             → Application packaging
 └ web                   → Compiled web assets
test                     → source folder for unit or functional tests

 

  app目录:

   app目录是代码目录,包含了所有的Java或者Scala的源码,一般的“hello-world”sample程序都含有controllers、models、和views三个目录,分别对应MVC三层结构中的:C、M和V;我想这大家都和清楚,大家还可以根据自己的项目需要创建其他的目录,例如utils、dao等等。例如以下:

如果有需要,你还可以建一个名为“assets”的目录,里面可以放LESS或者CoffeeScript源文件。

注意:这些controllers、models和views等目录可以随着你项目的需要而改变,例如:你可以写成com.yourcompany.controllers、com.yourcompnay.model和com.yourcompany.views而不必非得写成controllers、models和views。

conf目录:

在这个目录里,放置的都是这个应用的一些配置文件信息,有两个主要的文件:

一个是application.conf:意思很明显,就是整个应用的配置信息,里面会有一些配置的参数。包括数据库链接中数据源的信息填写,日志打印的级别等信息等等,还可以自定义一些参数。

注意:在conf中,play默认定义的有:数据库信息、应用信息(名字、 Secret key、语言等)、日志;这三块儿的信息,在conf中直接改后,效果会在应用程序中直接出现。

假如你想一用conf中自定义的配置参数:例如上图中的阿里云相关的信息,你需要在application.conf中定义之后,在程序中使用

Play.configuration.getString("oss.access_id").getOrElse("diSnug5q4zb9y2mq")

 

来调用。实际上某人的那三块信息也是这么来调用的。

假如你在application.conf中不想定义过多的自定义信息,你也可以写一个自定义的conf文件,然后在application.conf中引用(include “fileName.conf”)如下:

 

routes:路由。非常重要的部分!使用方法非常简单,在这里定义你需要的rest接口,然后接口后面对应的处理函数。如下图:

 

public 的目录:

这里放置的都是前端页面相关的信息,例如js、css、json文件、图片等等。

 

这些目录文件的名字是可以改的,但是引用的时候需要注意目录名字。包括public的名字也是可以改的。前端页面中需要其中的静态文件的话,需要再routes中添加:

然后在前端需要静态文件的地方这么引用:

这里就是用的public目录下images目录中的静态文件。

lib目录:

如果之前你是做J2EE项目的,这个目录你一定清楚,这就是放置其他依赖包的地方。(当然如果Maven有依赖链接,尽量用Maven的依赖链接)

build.sbt file:

这个文件是整个项目添加依赖包的地方,所有的依赖都写在这里。如果你是J2EE开发者的话,你一定知道Maven的pom.xml文件,在这里,build.sbt文件就相当于pom.xml的文件。

project目录:

这个目录包含了sbt构建之后的东西:

1、pulgins.sbt:插件sbt

2、build.properties:包含了sbt的版本。

target目录:

target目录包含了应用编译之后的东西,就是编译后的可执行文件。

 

更多精彩内容 请关注我的微信公众账号 互联网技术窝

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐