0%

This is just a quick introduction to the play framework using the Scala programming language.

Introduction

https://www.youtube.com/watch?v=t620S8-cwWg&list=PLBeQxJQNprbhb6kOJS477I_laHQTGgjg8&index=1

Create Project

sbt new playframework/play-scala-seed.g8

Enter projdet name scala-example.

HomeController.scala

app/controllers/HomeController.scala:

package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import play.api.libs.json._

@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents)
extends BaseController {

def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}

def ping = Action { implicit request =>
Ok("String works.")
}
def anotherOne = Action { _ =>
Ok(Json.obj("Yes" -> true))
}

def nameParam(name: String = "world") = Action { _ =>
Ok(Json.obj("hi" -> name))
}

def posted = Action(parse.json) { implicit request =>
Ok(Json.obj("received" -> Json.toJson(request.body)))
}
}

conf/routes:

# An example controller showing a sample home page
GET / controllers.HomeController.index
GET /ping controllers.HomeController.ping
GET /json controllers.HomeController.anotherOne
GET /hello/:name controllers.HomeController.nameParam(name:String)
POST /post controllers.HomeController.posted

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

Run:

sbt run

Test

Test with httpie :

http 127.0.0.1:9000
http 127.0.0.1:9000/ping
http 127.0.0.1:9000/json
http 127.0.0.1:9000/hello/vovo
http POST 127.0.0.1:9000/post phone="vovo"
http POST 127.0.0.1:9000/post < test.json

Data Model

Rebuild

https://www.youtube.com/watch?v=XD_X3BMsRhg&list=PLBeQxJQNprbhb6kOJS477I_laHQTGgjg8&index=4

scala-example> rm -rf app/views
scala-example> mv app/controllers/HomeController.scala app/controllers/AppController.scala

app/controllers/AppController.scala

package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import play.api.libs.json._

@Singleton
class AppController @Inject()(val controllerComponents: ControllerComponents)
extends BaseController {

def index() = Action { implicit request: Request[AnyContent] =>
Ok("App works!")
}

}

route

GET /   controllers.AppController.index
scala-example> http 127.0.0.1:9000

Result:

HTTP/1.1 200 OK
Content-Length: 10
Content-Type: text/plain; charset=UTF-8
Date: Sat, 11 Jan 2020 07:36:47 GMT
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Permitted-Cross-Domain-Policies: master-only
X-XSS-Protection: 1; mode=block

App works!

MongoDB

Search keyword mongo and reactivemongo:

libraryDependencies += "org.reactivemongo" %% "play2-reactivemongo" % "0.20.1-play27"

play.sbt.routes.RoutesKeys.routesImport += "play.modules.reactivemongo.PathBindables._"

application.conf

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
mongodb.uri = "mongodb://localhost:27017/scalarest"

Models

mkdir app/models
touch app/models/Post.scala

Service Layer

https://www.youtube.com/watch?v=_C5IlQgJbTo&list=PLBeQxJQNprbhb6kOJS477I_laHQTGgjg8&index=2

Create two files

scala-example> mkdir app/models
scala-example> touch app/models/Post.scala
scala-example> mkdir app/repositories
scala-example> touch app/repositories/PostRepository.scala

API Controller

https://www.youtube.com/watch?v=G6But_7ug9Q&list=PLBeQxJQNprbhb6kOJS477I_laHQTGgjg8&index=3

获取神器码

Plugins

IdeaVIM 插件的使用

./.ideavimrc:

inoremap jk <ESC>

" leader: ,
let mapleader = ","

" 映射idea常用快捷键
" 重命名
nnoremap <leader>r :action RenameElement<CR>
" 格式化当前文件
nnoremap <leader>f :action ReformatCode<CR><esc>
nnoremap <leader>q :action Close<CR><esc>
nnoremap <leader>i :action Generate<CR><esc>

" 插件模拟
" surround
set surround
" multiple-cursors
set multiple-cursors
" easyMotion 模拟,额外依赖插件:AceJump,IdeaVim-EasyMotion
set easymotion
" 注释插件模拟
set commentary

首先确保时间同步

配置科学上网折腾了我一天,windows和手机都很容易就连上了, 可是archlinux老是连接补上,配置改了又改,还是不行。最后发现本机时间和服务器时间有40秒钟的误差, 就抱着希望最后尝试一次,同步archlinux的时间, 结果奇迹发生了,可以连上了。

如果觉得配置麻烦,在这里生成配置:
https://intmainreturn0.com/v2ray-config-gen/

阅读全文 »

如果您看到这篇文章,我想您是对多线程(Multithreading )和并发(Concurrency)是很感兴趣的了。您可能在其他平台上已经初步了解了线程,现在想知道在 Java 平台上如何实现。我也和您一样,在学习 Java 和多线程。

这篇文章,将带您大致的了解多线程和并发。

基础

首先,要了解线程的基本原理,比如什么是进程(process),什么是线程(Threads),它们之间有什么区别。

阅读全文 »