play太好用了,表单用起来也是让我们的开发速度搜搜的。闲话少说,上demo。
简单的demo
1、在routes添加访问表单的路径
# Home form add by me
GET /form controllers.Application.form()
通过http://localhost:9000/form访问到我们的表单模板
2.添加application中处理form跳转的方法
public static Result form() {
return ok(form.render("winter you are bang"));
}
跳转到form.scala.html页面
3、添加form.scala.html页面
@(dong:String)
<!DOCTYPE html>
<html>
<head>
<title>@dong</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
</head>
<body>
<form method="POST" action="/postForm">
<label>Email</label>
<input type="email" name="email">
<label>Password</label>
<input type="password" name="password">
<label>Comment</label>
<input type="text" name="comment">
<input type="submit">
</form>
</body>
</html>
4.添加routes处理表单的
# Home form add by me postFormModel postForm
POST /postForm controllers.Application.postFormModel()
5、application中添加方法处理表格提交
public static Result postFormModel() {
String [] params = {};
DynamicForm bindFromRequest = Form.form().bindFromRequest(params);
String email = bindFromRequest.get("email");
String password = bindFromRequest.get("password");
String comment = bindFromRequest.get("comment");
System.out.println("email--"+email);
System.out.println("password--"+password);
System.out.println("comment--"+comment);
System.out.println(bindFromRequest);
return ok(dong.render("dong ge wei wu ","winter"));
}
大神的处理:
public static Result postFormModel() {
Form<User> userForm = Form.form(User.class);
User user = userForm.bindFromRequest().get();
return ok(dong.render(user.email ,user.password));
}
大神世界:
form.scala.html可以有程序生成:通过http://localhost:9000/form访问到我们的表单模板
@(userForm: Form[model.User])
<!DOCTYPE html>
<html>
<body>
@helper.form(action = routes.Application.postFormModel()) {
@helper.inputText(userForm("email"))
@helper.inputPassword(userForm("password"))
@helper.inputText(userForm("comment"))
<input type="submit">
}
</body>
</html>
model的user
package model;
import play.data.validation.Constraints.Email;
import play.data.validation.Constraints.Required;
public class User {
@Email
public String email;
@Required
public String password;
public String comment;
}