实现客户端验证:设计详细信息

此样例应用程序说明了如何使用 JavaServer[TM] Faces 技术进行客户端验证。该应用程序位于 java.net 上的 bpcatalog 项目下。

该应用程序包含两个 JavaServer 页:一个是 index.jsp 文件,另一个是 response.jsp 文件。index.jsp 页在一个页面中包含了所有 JavaServer Faces 代码示例。如果页面中的数据有效,会将请求转发到 response.jsp 页;如果验证失败,则会返回到显示错误消息的 index.jsp 页。

本设计文档说明了该应用程序的一些页面、源文件和配置文件:

ColorRenderer

定制呈现器 ColorRenderer 通过其 encodeEnd 方法将 client_validation JavaScript[TM] 函数呈现给 index.jsp 文件,下面显示了该方法的部分代码:

    public void encodeEnd(FacesContext context, UIComponent component)
        throws IOException {

        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }

        UIOutput script = (UIOutput) component;
        UIInput currentColor = (UIInput) component.findComponent("color");
        ResponseWriter writer = context.getResponseWriter();

        StringBuffer sb = null;
        System.out.println("currentColor =" + currentColor);
        if (currentColor == null) return;
        sb = new StringBuffer("function client_validation() {\n");
        sb.append("if (document.forms[0]['");
        sb.append(currentColor.getClientId(context));
        sb.append("'].value=='red' ) {").append("\n");
        sb.append("return true;");
        sb.append("\n} else if (document.forms[0]['");
        sb.append(currentColor.getClientId(context));
        sb.append("'].value=='green' ) {").append("\n");
        sb.append("return true;");
        sb.append("\n} else if (document.forms[0]['");
        sb.append(currentColor.getClientId(context));
        sb.append("'].value=='blue' ) {").append("\n");
        sb.append("return true;");
        sb.append("\n } else {\n");
        sb.append("alert('Enter red, green, or blue for the color.');");
        sb.append("\nreturn false;\n");
        sb.append("} \n }");
        sb.append("\n");
...

ScriptTag

由于创建了新的组件/呈现器组合,因此还需要创建一个定制标记,在页面上表示该组合。在本例中,定制标记称为 script。当 JavaServer Page[TM] (JSP[TM]) 页转换为 HTML 后,script 标记将转换为执行客户端验证的 JavaScript。ScriptTag 类是定制 script 标记(它显示 ColorRenderer 呈现的 JavaScript)的标记处理程序。

ColorValidator

该类执行服务器端验证,如下所示:

 public void validate(FacesContext context, UIComponent component, Object toValidate) {
String value = null;
if ((context == null) || (component == null)) {
throw new NullPointerException();
}
if (!(component instanceof UIInput)) {
return;
}
if ( null == toValidate) {
return;
}

value = toValidate.toString();

if (value.equals("red") || value.equals("green") || value.equals("blue")) {
return;
} else {
System.out.println("value "+value);
FacesMessage errMsg = MessageFactory.getMessage(context,
COLOR_INVALID_MESSAGE_ID);
throw new ValidatorException(errMsg);
}
}

customTags.tld

要创建定制 script 标记,您需要为其创建标记处理程序类并在 TLD 文件中定义该标记。ScriptTag 是 script 标记的标记处理程序。script.tld 文件用于定义 script 标记。此 TLD 定义显示 JavaScript 的定制 script 标记。标记定义如下显示:

<tag>
<name>script</name>
<tag-class>com.sun.j2ee.blueprints.catalog.validator.ScriptTag</tag-class>
<body-content>empty</body-content>
...
<attribute>
<name>language</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>String</type>
</attribute>
</tag>

faces-config.xml

要在应用程序中使用定制呈现器和验证器,必须在该配置文件中注册它们。下面是配置文件中注册定制呈现器和验证器的元素:

  <render-kit>
    ...
    <renderer>
...
      <component-family>javax.faces.Output</component-family>
      <renderer-type>Color</renderer-type>
      <renderer-class>com.sun.j2ee.blueprints.catalog.validator.ColorRenderer
</renderer-class>
    </renderer>
</render-kit>

 <validator>
...
<validator-id>ColorValidator</validator-id>
<validator-class>com.sun.j2ee.blueprints.catalog.validator.ColorValidator
</validator-class>
<attribute>
<attribute-name>color</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
</validator>


index.jsp

此页包含了其数据要经过验证的输入组件。它在输入组件上注册了服务器端验证器 ColorValidator,同时显示了对此组件的数据执行客户端验证的 JavaScript。

下面是注册了定制服务器端验证器的输入组件:
<h:inputText id="color" >
          <f:validator validatorId="ColorValidator" />
</h:inputText>

下面是定制 script 标记,它可在页面转换成 HTML 时显示定制呈现器 ColorRenderer 呈现的 JavaScript:

<customTags:script/>

要使用 script 标记,则该页需要包含从 JSP 页引用它的 taglib 指令:

<%@ taglib prefix="customTags" uri="WEB-INF/customTags.tld" %>

表单标记的 onsubmit 属性调用 client_validation JavaScript 方法:

<h:form id="validatorForm" onsubmit="return client_validation();" />

© Sun Microsystems 2004。Java BluePrints Solutions Catalog 中的所有内容受版权保护,未经 Sun Microsystems 的明确书面许可,不得在其他产品中发布。