Skip to main content

4. JSF: Validating Inputs. Part 1/2

0. Introduction


As Marty Hall indicates, there are several resources for validating inputs:

1. required="true" requiredMessage="…" for fields that should be filled.

2. converterMessage="..." for no-string fields that require a conversion.

3. f:validateBlah tags, validatorMessage for values in a range or that matches a regular expression.

4. h:message, h:messages for values displayin messages.

5. Validating manually by means of the action controller.

6. h:inputBlah validator="#{somebean.somemethod}" using a custom validator.

7. Using Apache MyFaces for URL and other stuff validation.(problematic!!!)



1. A simple form with different validations


Here is a simple form using the first 6 validators. Its name is:
   test-05-validation-page.xhtml



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:p="http://primefaces.org/ui">  
    <h:head>  
    </h:head>  
      
    <h:body>  
      <h:form>  
        <h2>VALIDATION EXAMPLE</h2>
        <h:outputText value="Fix Errors Marked Below" styleClass="error"
                      rendered="#{not empty facesContext.messageList}"/>
        <p:panelGrid  columns="3">  
          <!-- First line -->  
    #{label.user}
       <h:inputText value="#{loginBean.user}" id="f01"
                    required="true" requiredMessage="#{msg.required}"/>
       <h:message for="f01"/>
          
          <!--Second line --> 
          #{label.password}
          <h:inputSecret value="#{loginBean.password}" id="f02"></h:inputSecret>
          <h:message for="f02"/>
          
          <!--Third line --> 
          #{label.intnum}
          <h:inputText value="#{conversionBean.anyInteger}" id="f03"
                       converterMessage="#{msg.intconvserr}"/> 
          <h:message for="f03"/>
          
          <!--Forth line --> 
          #{label.dblnum}
          <h:inputText value="#{conversionBean.anyDouble}" id="f04"
                       converterMessage="#{msg.dblconvserr}"/>  
          <h:message for="f04"/>
          
          <!--Fifth line --> 
          Other integer from 2 to 10
          <h:inputText value="#{conversionBean.anyInteger2}" id="f05"
                       validatorMessage="Amount must be between 2 and 10">
            <f:validateLongRange minimum="2" maximum="10">
            </f:validateLongRange>
          </h:inputText>               
          <h:message for="f05"/>
          
          <!--Sixth line --> 
          Other double from 2.1 to 10.9
          <h:inputText value="#{conversionBean.anyDouble2}" id="f06"
                       validatorMessage="Amount must be between 2.1 and 10.9">
            <f:validateDoubleRange minimum="2.1" maximum="10.9">
            </f:validateDoubleRange>
          </h:inputText>               
          <h:message for="f06"/>
         
          <!--Seventh line --> 
          A string of length between 3 and 5
          <h:inputText value="#{conversionBean.string3_5}" id="f07"
                       validatorMessage="String length between 3 and five">
            <f:validateLength minimum="3" maximum="5">
            </f:validateLength>
          </h:inputText>               
          <h:message for="f07"/>
          
          <!--Eight line --> 
          A string with pattern [A-Z][A-Z]
          <h:inputText value="#{conversionBean.stringRegex}" id="f08"
                       validatorMessage="String regex should be accomplished">
            <f:validateRegex pattern="[A-Z][A-Z]">
            </f:validateRegex>
          </h:inputText>               
          <h:message for="f08"/>
          
                 
        </p:panelGrid>
        <h:commandButton value="Login" action="#{loginBean.login}"/>
                         
      </h:form>
    </h:body>  
</html>
NOTE:

  1. msg and label are variables that retrieve values from resource bundle (labels.properties and msgs.properties). These variables should be declared in faces-config.xml.

  2. loginBean and conversionBean are the referenced beans the form.

  3. requiredMessageconverterMessage and validatorMessages are attributes of the jsf components to reference the messages to display when an invalid input is supplied. They refer to blank input, conversion error, and validation condition violation respectively.

  4. Facelets tags <f:validateBlah> where Blah can be Length, LongRange, DoubleRange and Regex, are used to settle conditions to string length, long and double intervals, and pattern matching strings respectively.

  5. rendered="#{not empty facesContext.messageList}" is a condition to render the control when there are no messages to display.

  6. Validation is performed before executing the button action.

The LoginBean class is the same as the one in the last post.

The new simple bean ConversionBean.java is


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.ximodante.jsf.validation;

import java.io.Serializable;

import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;

/**
 * For numeric conversion use objects and no primitives
 *   Use Double, Integer instead of double and int
 * @author Ximo Dante
 *
 */
@Named
public class ConversionBean implements Serializable {
 
 
 private static final long serialVersionUID = 20171024L;
 @Getter @Setter private Integer anyInteger=0;
 @Getter @Setter private Double anyDouble= 0.0;
 @Getter @Setter private Integer anyInteger2=-1;
 @Getter @Setter private Double anyDouble2= -50.0;
 @Getter @Setter private String string3_5;
 @Getter @Setter private String stringRegex;
}


The faces-config.xml is


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 
              xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
              version="2.2">  
              
  <application>
    
    <resource-bundle>
      <base-name>bundles.labels</base-name>
      <var>label</var>
    </resource-bundle>
    
    <resource-bundle>
      <base-name>bundles.msgs</base-name>
      <var>msg</var>
    </resource-bundle>
  </application>

</faces-config>


And note that the file msgs.properties with its locale variants (msgs_es.properties ...)  have been created in the same folder as folder as the labels.properties file (in the src/main/java/bundles folder)

An image of the page and the project is shown



Comments

Popular posts from this blog

10. JSF How to access my resources files?

Sometimes it is really difficult to access any resources form different Java applications. So let's see some scenarios: 1. Executable jar application in a NO WEB environment. Take into account that this is not a JSF application !!! It is desired to have a property file outside the "jar file" so that we can change properties when needed. For instance, we will place the properties file in the same folder that the "jar file" lies. To access the folder where the jar is, the trick is to access the folder where the compiled classed are placed and scale 3 folders . (Oh! rather strange, if anybody knows a better idea.., he will be welcome). Here is a simple code. The name of the class containing the code is ThisClass, and we have created a Property class that feeds with a file in the same folder that the" jar file" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /** * Gets the folder where resides the executable...

9. JSF: Solving common problems

1. Target Unreachable, identifier [bean] resolved to null When you reference a bean in a xhtml file with "#{bean.method}" and get this error, you should verify that: The bean implements Serializable . The references (javax.servlet-api.4.0.0, javax.faces.2.3.3,  javax.el.api.3.0.1, org.primefaces.6.1) have been selected in your pom.xml You have created the files bean.xml and faces-config.xml in the webapp/WEB-INF folder You have used the correct annotations from CDI  (javax.inject.Named and javax.enterprise.context.SessionScoped, ...) and not ManagedBean or jsf2.2 scopes. 2. Bean declaring a passivating scope must be passivation capable This error causes server not starting and fills up the console with long chained exceptions. It is very annoying. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to star...

2. Create a Maven JSF project and a simple login page

Updated on Oct-25-2107 0. Introduction Remember to install previously: Java JDK 8 Eclipse Oxygen Lombok  Apache Tomcat 9 1. Create a Maven Project In the top menu select : File - New - Maven Project Press Next Now fill the next form as follows It is important to select: Group id: org.ximodante.jsf  (or another packet you like) Packaging: war Artifact Id and Name: JSFv02 or any name for the project Press Finish 2. The pom.xml file Eclipse complains about the simple generated file. It seems that a web.xml is required if the packaging is selected to war. The simple generated file is  1 2 3 4 5 6 7 8 9 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <groupId> org.ximodante...