play_scala框架學習04 表單

作者:Vamei 出處:http://www.cnblogs.com/vamei 歡迎轉載,也請保留這段聲明。謝謝!

 

表單(form)是最常見的從客戶往服務器傳遞數據的方式。Play框架提供了一些工具。它們可以從表單中提取數據,驗證提交數據的合法性,或者在視圖中顯示錶單。我先來介紹最簡單的使用表單提交數據的方式。 

 

增加表單

我可以用純粹html的方式產生一個表單。在app/views下增加模板form.scala.html:

複製代碼
<!DOCTYPE html>
<html>
  <body>
    <form method="POST" action="/postForm">
      <input type="text" name="content"></input>
      <input type="submit"></input>
    </form>
  </body>
</html>
複製代碼

 

在Application控制器中,增加一個動作form(),顯示模板:

public static Result form() {
    return ok(views.html.form.render());
}

 

在routes中增加導航

GET     /form                       controllers.Application.form()

 

頁面如下:

 

數據提取

在文本框中輸入任意字符,點擊submit後,表單將以POST方法提交到/postForm這一URL。增添負責處理該URL的動作,Application.postForm()

public static Result postForm() {
    DynamicForm in   = Form.form().bindFromRequest();
    String result    = in.get("content");
    return ok(result);
}

DynamicFormForm都來自play.data。Form.form().bindFormRequest()從請求中提取表單信息,並放入到DynamicForm類型的in對象中。

我上面用get()方法,來提取表單中不同名字的輸入欄。比如上面的"content"。postForm()動作把表單中填寫的內容直接顯示。

 

增加routes記錄

POST    /postForm                   controllers.Application.postForm()

 

在/form的頁面下輸入任意字符串並提交,查看效果。

 

我介紹了表單最基本的使用方式。下面瞭解Play框架提供的其它的表單工具。

 

表單對象

在動作內部,可以創建一個對象來指代表單。表單的每個輸入欄爲表單對象的一個屬性。我可以通過增加標註(annotation)的方法,驗證表單的輸入(Form Validation)。

 

首先修改app/views/form.scala.html

複製代碼
<!DOCTYPE html>
<html>
  <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>
複製代碼

這個表單有三個輸入欄,名字分別爲email, password和comment。

 

創建app/util/文件夾,在其中創建User.java。User類用於在Play內部指代上面的表單:

複製代碼
package util;

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;
}
複製代碼

User類指代一個表單的數據。我還爲兩個屬性增加了標註。Play服務器可以據此驗證輸入的合法性。比如@Email的限定就要求輸入爲"*@*"的形式。@Required則要求輸入欄不爲空。如果違反這些限定,那麼Play將拋出異常。

 

修改動作postForm()。User類的對象user用來保存表單數據。

public static Result postForm() {
    Form<User> userForm = Form.form(User.class);
    User user = userForm.bindFromRequest().get();
    return ok(user.email + " " + user.password);
}

最後的ok()中調用了表單對象中保存的數據。 

 

分別輸入合法和不合法的數據,觀察Play返回的頁面。

 

表單模板

我上面手動創建模板中的表單,並保持視圖中的表單和表單對象一致。我還可以在模板中直接調用表單對象。這樣做,能讓視圖中的表單和表單對象自動的保持一致。

修改form.scala.html爲

複製代碼
@(userForm: Form[util.User])

<!DOCTYPE html>
<html>
  <body>
    @helper.form(action = routes.Application.postForm()) {
      @helper.inputText(userForm("email"))
      @helper.inputPassword(userForm("password"))
      @helper.inputText(userForm("comment"))
      <input type="submit">
    }
  </body>
</html>
複製代碼

這裏使用了Play所提供的helper工具。helper可以在表單中增加表單form,再加入不同類型的輸入欄,比如inputText和inputPassword。

 

修改原有的動作form()

public static Result form() {
    Form<User> userForm = Form.form(User.class);
return ok(views.html.form.render(userForm)); }

這裏,表單對象作爲參數傳遞給模板。最後的html頁面中的表單,將由Play自動生成。

 

總結

表單

數據提交

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章