论坛首页 Java版

tapestry-core :Form Input and Validation

浏览 410 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-05-09 关键字: Tapestry5

本文翻译出处 http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html
本人翻译目的是用来学习Tapestry5的,共享出来希望大家批评指正。计划持续翻译。

chinajavawolf

 

Form Input and Validation

表单输入和验证

The life's blood of any application is form input; this is the most effective way to gather significant information from the user. Whether it's a search form, a login screen or a multi-page registration wizard, forms are how the user really expresses themselves to the application.

对于任何应用表单输入都是必不可少的;这是更有效的从用户处收集有用信息的方法。不管它是一个搜索表单、一个登录窗口或是一个多页的注册向导,用户使用表单真正向应用表达自己。

Tapestry excels at creating forms and validating input. Input validation is declarative, meaning you simply tell Tapestry what validations to apply to a given field, and it takes care of it on the server and (once implemented) on the client as well.

Tapestry在创建表单和验证输入方面表现杰出。输入验证是声明式的,这意味着你只是告诉Tapestry在给出的那个表单域上应用什么验证,然后他就会在服务器端和客户端负责验证。

Finally, Tapestry is able to not only present the errors back to the user, but to decorate the fields and the labels for the fields, marking them as containing errors (primarily, using CSS effects).

最后,Tapestry不仅能够将错误的信息表现给用户,还能对表单域和表单域地标签进行装饰,标记它们包含错误(主要利用CSS效果)。

Form component

表单组件

The core of Tapestry's form support is the Form component. The Form component encloses (wraps around) all the other field components such as TextField, TextArea, Checkbox, etc.

Tapestry表单核心支持表单组件。表单组件封装所有其他表单域组件,如TextField, TextArea, Checkbox等等。

The Form component generates a number of component events that you may provide event handler methods for.

表单组件产生许多的组件事件component events,你可以提供事件处理器方法给他。

When rendering, the Form component emits a "prepare" notification, to allow the Form's container to setup any fields or properties that will be referenced in the form. For example, this is a good chance to create a temporary entity object to be rendered, or to load an entity from a database to be editted.

当呈现的时候,表单组件发布一个”prepare”通知,以使表单容器设置将要在表单中引用的表单域或属性。这是一个创建用来呈现的临时实体对象或用来加载来自于数据库的可编辑的实体的好时机。

When user submits the form on the client, a series of steps occur on the server.

当用户在客户端提交表单时,服务器端会执行一系列的步骤。

First, the Form emits a "prepare" notification, as it did when the Form was rendered.

首先,当表单被呈现时表单会发出一个"prepare"通知。

Next, all the fields inside the form are activated to pull values out of the incoming request, validate them and (if valid) store the changes.

接下来,所有的表单域被激活从引用的请求处获值,验证它们并(如果有效)保存变化。

For Tapestry 4 Users: Tapestry 5 does not use the fragile "form rewind" approach from Tapestry 4. Instead, a hidden field generated during the render stores the information needed to process the form submission.

Tapestry 4的用户:Tapestry 5不使用Tapestry 4中脆弱的"form rewind"方法,而是用在呈现时产生一个存放是否需要处理表单提交信息的隐藏域来代替。

After the fields have done their processing, the Form emits a "validate" event. This is a chance to perform cross-form validation that can't be described declaratively.

表单域流程处理完后,表单发出一个"validate"事件,这是执行跨表单验证的不能明白地被描述的一个机会。

Next, the Form determines if there have been any validation errors. If there have been, then the submission is considered a failure, and a "failure" event is emitted. If there have been no validation errors, then a "success" event is emitted.

接下来,表单确定是否存在任何验证错误。如果存在,那么提交一个被评估过的失败并发出一个"failure"事件。如果没有验证错误,那么将发出一个"success"事件。

Last, the Form emits a "submit" event (for logic that doesn't care about success or failure).

最后,表单发出一个"submit"事件(逻辑上它不考虑成功还是失败)。

Tracking Validation Errors

跟踪验证错误

Associated with the Form is an ValidationTracker that tracks all the provided user input and validation errors for every field in the form. The tracker can be provided to the Form via the Form's tracker parameter, but this is rarely necessary.

一个与表单关联的就是验证跟踪器ValidationTracker跟踪用户所有对每个表单域提供的书如何验证错误。跟踪器能够通过表单的跟踪器参数提供给表单,但这不是必需的。

The Form includes methods isValid() and getHasErrors(), which are used to see if the Form's validation tracker contains any errors.

表单包括isValid() getHasErrors()方法,他们被用来了解表单验证的跟踪器中是否包含错误。

In your own logic, it is possible to record your own errors. Form includes two different versions of method recordError(), one of which specifies a Field (an interface implemented by all form element components), and one of which is for "global" errors, unassociated with any particular field.

在你自己的逻辑中,你可以去纪录你自己的错误。表单包括两个不同的recordError()方法版本。一个是指定一个表单域(一个被所有表单元素组件实现的接口),另外一个是全局"global"错误,与具体的表单域无关。

Storing Data Between Requests

请求间存储数据

As with other action requests, the result of a form submission is to send a redirect to the client which re-renders the page. The ValidationTracker must be stored persistently between requests, or all the validation information will be lost (the default ValidationTracker provided by the Form is persistent).

由于其他的动作请求,表单提交的结果会发送给客户端一个重定向去重新呈现页面。验证跟踪器必须在请求间被持久化地存储,否则所有的验证信息将被丢失(表单提供持久化式的默认验证跟踪器)。

Likewise, the individual fields updated by the components should also be persistent.

同样,被组件更新的单独的表单域也应该被持久化。

For example, a Login page, which collects a user name and a password, might look like:

例如,一个用来收集用户名和密码登陆页,如下:

Because of the the fact that a form submission is two requests (the submission itself, then a re-render of the page), it is necessary to make the value stored in the _userName field persist between the two requests. This would be necessary for the _password field as well, except that the PasswordField component never renders a value.

因为Form表单提交事实上是两个请求(提交自己,然后重新呈现页面),他需要在两个请求间产生一个值持续存储在_userName属性中。同样_password属性也应该被这样做,除非PasswordField组件从不呈现值。

Note that the onSuccess() method is not public; event handler methods can have any visibility, even private. Package private (that is, no modifier) is the typical use, as it allows the component to be tested, from a test case class in the same package.

注意onSuccess()不是公有的;事件处理器方法可以有任何可见度,甚至是私有的。包私有(就是没有修饰符)是典型应用,这样它允许组件可被相同包下的测试用例类测试。

The Form only emits a "success" event if the there are no prior validation errors. This means it is not necessary to write if (_form.getHasErrors()) return; as the first line of the method.

如果先前没有验证错误,表单就只发出一个"success"事件。这意味着不用写

if (_form.getHasErrors()) return;在方法的第一行。

Finally, notice how business logic fits into validation. The UserAuthenticator service is responsible for ensuring that the userName and (plaintext) password are valid. When it returns false, we ask the Form component to record an error. We provide the PasswordField instance as the first parameter; this ensures that the password field, and its label, are decorated when the Form is re-rendered, to present the errors to the user.

最后,注意业务逻辑如何与表单验证相适合。UserAuthenticator服务负责userName<span lang="EN-US" style="FONT-SIZE: 9pt; BACKGROUND: lime; COLOR: black; FONT-FAMI

  1. public class Login   
  2.   
  3. {   
  4.   
  5.     @Persist  
  6.   
  7.     private String _userName;   
  8.   
  9.     
  10.   
  11.     private String _password;   
  12.   
  13.     
  14.   
  15.     @Inject  
  16.   
  17.     private UserAuthenticator _authenticator;   
  18.   
  19.     
  20.   
  21.     @Component(id = "password")   
  22.   
  23.     private PasswordField _passwordField;   
  24.   
  25.     
  26.   
  27.     @Component  
  28.   
  29.     private Form _form;   
  30.   
  31.     
  32.   
  33.     String onSuccess()   
  34.   
  35.     {   
  36.   
  37.         if (!_authenticator.isValid(_userName, _password))   
  38.   
  39.         {   
  40.   
  41.             _form.recordError(_passwordField, "Invalid user name or password.");   
  42.   
  43.             return null;   
  44.   
  45.         }   
  46.   
  47.     
  48.   
  49.         return "PostLogin";   
  50.   
  51.     }   
  52.   
  53.     
  54.   
  55.     public String getPassword()   
  56.   
  57.     {   
  58.   
  59.         return _password;   
  60.   
  61.     }   
  62.   
  63.     
  64.   
  65.     public void setPassword(String password)   
  66.   
  67.     {   
  68.   
  69.         _password = password;   
  70.   
  71.     }   
  72.   
  73.     
  74.   
  75.     public String getUserName()   
  76.   
  77.     {   
  78.   
  79.         return _userName;   
  80.   
  81.     }   
  82.   
  83.     
  84.   
  85.     public void setUserName(String userName)   
  86.   
  87.     {   
  88.   
  89.         _userName = userName;   
  90.   
  91.     }   
  92.   
  93. }   
  94.   
   
论坛首页 Java版

跳转论坛:
JavaEye推荐