日期:2014-05-16  浏览次数:20304 次

JSF2简单验证------JSF2 学习笔记2

1.准备工作 如 一个简单的jsf例子------JSF2学习笔记1

2. 不一样的是index.jsp? 代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
   <f:view>
      <head>                  
         <title>index</title>
      </head>
      <body>
         <h:form>
            <h3>Please enter your name and password.</h3>
            <table>
               <tr>
                  <td>Name:</td>
                  <td>
                     <h:inputText value="#{user.name}" required="true" id="name" requiredMessage="请输入用户名"/>
                     <span style="color: red">*</span>
                     <h:message for="name"></h:message> 
                  </td>
               </tr>             
               <tr>
                  <td>Password:</td>
                  <td>
                     <h:inputSecret value="#{user.password}" required="true" id="password" requiredMessage="请输入密码"/>
                     <span style="color: red">*</span>
                     <h:message for="password"></h:message> 
                  </td>
               </tr>
            </table>
            <p>
               <h:commandButton value="Login" action="login"/>
            </p>
         </h:form>
      </body>
   </f:view>
</html>

?

3.JSF的标准验证器

?

f:validateLongRange

<h:inputText value="#{userRegister.age }"  required="true" id="Age">
	<f:validateLongRange minimum="1" maximum="150"></f:validateLongRange>	<!-- 年龄在1到150之间 -->
</h:inputText>
<h:message for="Age"></h:message>

?

f:validateDoubleRange

<h:inputText value="#{userRegister.height }" required="true" id="Height">
	<f:validateDoubleRange minimum="75.00" maximum="250.00"></f:validateDoubleRange>
</h:inputText>
<h:message for="Height"></h:message>

?

?