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> |
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. requiredMessage, converterMessage 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
Post a Comment