目录

f:validateRegex

f:validateRegex标记用于将字符串值验证为所需格式。

JSF标签 (JSF Tag)

<f:validateRegex pattern = "((?=.*[a-z]).{6,})" />

标签属性 (Tag Attributes)

S.No 属性和描述
1

pattern

格式化模式

例子 Example Application

让我们创建一个测试JSF应用程序来测试上面的标记。

描述
1com.iowiki.test包下创建一个名为helloworld的项目,如JSF - First Application一章中所述。
2 修改home.xhtml ,如下所述。 保持其余文件不变。
3 在webapps目录中创建result.xhtml ,如下所述。
4com.iowiki.test包下创建UserData.java作为托管bean,如下所述。
5 编译并运行应用程序以确保业务逻辑按照要求运行。
6 最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Webserver中。
7 使用适当的URL启动Web应用程序,如下面的最后一步所述。

UserData.java

package com.iowiki.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
   private static final long serialVersionUID = 1L;
   private String password;
   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }   
}

home.xhtml

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"   
   xmlns:h = "http://java.sun.com/jsf/html"
   xmlns:f = "http://java.sun.com/jsf/core">
   <h:head>
      <title>JSF tutorial</title>			
   </h:head>
   <h:body>    
      <h2>h:validateRegex Example</h2>
      <!-- password contains lower case letters only and.
      length of the password should be greater than 6. -->
      <h:form>
         <h:inputSecret id = "passwordInput" value = "#{userData.password}" 
            label = "password" >
            <f:validateRegex pattern = "((? = .*[a-z]).{6,})" />
         </h:inputSecret>			
         <h:commandButton value = "submit" action = "result"/>
         <h:message for = "passwordInput" style = "color:red" />
      </h:form>
   </h:body>
</html>

result.xhtml

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html">
   <h:head>
      <title>JSF Tutorial!</title>   
   </h:head>
   <h:body>
      <h2>Result</h2>
      <hr />     
      Password: #{userData.password}     
   </h:body>
</html>  

一旦准备好完成所有更改,让我们像在JSF - First Application章节中那样编译和运行应用程序。 如果您的应用程序一切正常,这将产生以下结果。

JSF f:validateRegex

输入无效值。 以下是输出。

JSF f:validateRegex1

输入有效值。 以下是输出。

JSF f:validateRegex2
↑回到顶部↑
WIKI教程 @2018