JSF - f: validateLength

f: la balise validateLength est utilisée pour valider la longueur d'une valeur de chaîne dans une plage particulière.

Balise JSF

<f:validateLength minimum = "5" maximum = "8" />

Attributs de balise

S. Non Attribut et description
1

minimum

Une chaîne avec un nombre minimum de caractères

2

maximum

Une chaîne avec un nombre maximum de caractères

Exemple d'application

Créons une application JSF de test pour tester la balise ci-dessus.

Étape La description
1 Créez un projet avec un nom helloworld sous un package com.tutorialspoint.test comme expliqué dans le chapitre JSF - Première application .
2 Modifiez home.xhtml comme expliqué ci-dessous. Gardez le reste des fichiers inchangés.
3 Créez result.xhtml dans le répertoire webapps comme expliqué ci-dessous.
4 Créez UserData.java en tant que bean géré sous le package com.tutorialspoint.test comme expliqué ci-dessous.
5 Compilez et exécutez l'application pour vous assurer que la logique métier fonctionne conformément aux exigences.
6 Enfin, créez l'application sous la forme d'un fichier war et déployez-la dans Apache Tomcat Webserver.
sept Lancez votre application Web en utilisant l'URL appropriée, comme expliqué ci-dessous à la dernière étape.

UserData.java

package com.tutorialspoint.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 name;
  
   public String getName() {
      return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }  
}

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:validateLength Example</h2>
      
      <h:form>
         <h:inputText id = "nameInput" value = "#{userData.name}" 
            label = "name" >
            <f:validateLength minimum = "5" maximum = "8" />
         </h:inputText>			
         <h:commandButton value = "submit" action = "result"/>
         <h:message for = "nameInput" 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 />
      Name: #{userData.name}     
   </h:body>
</html>

Une fois que vous êtes prêt avec tous les changements effectués, laissez-nous compiler et exécuter l'application comme nous l'avons fait dans le chapitre JSF - Première application. Si tout va bien avec votre application, cela produira le résultat suivant.

Entrez une valeur non valide. Voici la sortie.

Entrez une valeur valide. Voici la sortie.