首页  

play框架web编程实例     所属分类 play 浏览量 505
HelloController.scala
LoginController.scala
login.scala.html
conf/routes



测试 -Dhttp.port=9100 http://127.0.0.1:9100/hello http://127.0.0.1:9100/hello2?name=tiger http://127.0.0.1:9100/doLoginSimple?userName=hello&password=java http://127.0.0.1:9100/doLogin?userName=53732908&password=12345678 List(FormError(userName,List(error.email),List())) http://127.0.0.1:9100/doLogin?userName=53732908&password=12345 List(FormError(userName,List(error.email),List()), FormError(password,List(error.minLength),ArraySeq(6))) http://127.0.0.1:9100/doLogin?userName=53732908@qq.com&password=1234567 53732908@qq.com&1234567
HelloController.scala package controllers import play.api.mvc._ import java.time.LocalDateTime import javax.inject._ @Singleton class HelloController @Inject()(cc: ControllerComponents) extends AbstractController(cc) { def hello() = Action { implicit request: Request[AnyContent] => var name = request.getQueryString("name").getOrElse(null); if (name == null) { name = "scala" } Ok("hello " + name + "," + LocalDateTime.now()) } }
LoginController.scala package controllers import play.api.data.Forms._ import play.api.data._ import play.api.mvc._ import javax.inject._ @Singleton class LoginController @Inject()(cc: ControllerComponents) extends AbstractController(cc) { def login = Action { Ok(views.html.login("user login")) // Ok("login") } def doLoginSimple(userName: String, password: String) = Action { val mess = userName + "&" + password Ok(mess) } def doLogin = Action { implicit request => val loginForm = Form( tuple( "userName" -> email, "password" -> text(minLength = 6) ) ) loginForm.bindFromRequest().fold( errorForm => Ok(errorForm.errors.toString()), tupleData => { val (userName, password) = tupleData Ok(userName + "&" + password) } ) } }
@(title: String) <!DOCTYPE html> <html lang="en"> <head><title>@title</title></head> <body> <form action="/doLogin"> <input name="userName" type="text" placeholder="User Name" /> <input name="password" type="password" placeholder="Password" /> <button type="submit">登陆</button> </form> </body> </html>
GET /login controllers.LoginController.login GET /doLogin controllers.LoginController.doLogin GET /doLoginSimple controllers.LoginController.doLoginSimple(userName: String, password: String)
完整代码 https://gitee.com/dyyx/hellocode/tree/master/demo/scala/play-rest-hello

上一篇     下一篇
Scala 下划线的用途

scala 匿名函数

scala函数定义及使用

scala 大括号省略

scala 表达式/代码块 当参数

play action 各种写法