reffec.com

Customizing JSF"s default messages in Software Access qrcode in Software Customizing JSF"s default messages

6. generate, create denso qr bar code none on software projects Microsoft Official Website StringUtils In the example, Software QR Code ISO/IEC18004 we used org.apache.commons.

lang.StringUtils to perform the actual validation logic. In addition to the method used in the example, this class contains several methods for verifying that a string is numeric or alphanumeric.

This class, part of the Apache commons-lang library, is very useful when writing custom validators.. As every validator method mu st be in a managed bean, we need to make sure that the class containing our validator method is annotated with the @ManagedBean annotation, as illustrated in our example. The last thing we need to do to use our validator method is to bind it to our component via the tag"s validator attribute:. < xml version="1.0" encod QR Code ISO/IEC18004 for None ing="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>Enter Customer Data</title> </h:head> <h:body> <h:outputStylesheet library="css" name="styles.

css" target="body"/> <h:form> <h:messages></h:messages> <h:panelGrid columns="2" columnClasses="rightAlign,leftAlign"> <h:outputText value="First Name:"> </h:outputText> <h:inputText label="First Name" value="#{customer.firstName}" required="true" validator="#{alphaValidator.validateAlpha}"> <f:validateLength minimum="2" maximum="30"> </f:validateLength> </h:inputText> <h:outputText value="Last Name:"></h:outputText> <h:inputText label="Last Name" value="#{customer.

lastName}" required="true" validator="#{alphaValidator.validateAlpha}"> [ 223 ]. JavaServer Faces <f:valid ateLength minimum="2" maximum="30"> </f:validateLength> </h:inputText> <h:outputText value="Email:"> </h:outputText> <h:inputText label="Email" value="#{customer.email}"> <f:validateLength minimum="3" maximum="30"> </f:validateLength> <f:validator validatorId="emailValidator" /> </h:inputText> <h:panelGroup></h:panelGroup> <h:commandButton action="confirmation" value="Save"> </h:commandButton> </h:panelGrid> </h:form> </h:body> </html>. As neither the First Name no Software QR Code ISO/IEC18004 r the Last Name fields should accept anything other than alphabetic characters or spaces, we added our custom validator method to both these fields. Notice that the value of the validator attribute of the <h:inputText> tag is a JSF expression language using the default managed bean name for the bean containing our validation method. alphaValidator is the name of our bean and validateAlpha is the name of our validator method.

After modifying our page to use our custom validator, we can now see it in action:. Notice how for the First Nam e field, both our custom validator message and the standard length validator were executed.. [ 224 ]. 6 . Implementing validator metho QR-Code for None ds has the advantage of not having the overhead of creating a whole class just for a single validator method (our example does just that, but in many cases, validator methods are added to an existing managed bean containing other methods). However, the disadvantage is that each component can only be validated by a single validator method. When using validator classes, several <f:validator> tags can be nested inside the tag to be validated.

Therefore, multiple validations, both custom and standard, can be done on the field.. Customizing JSF"s default messages Like we mentioned in the pre Software Denso QR Bar Code vious section, it is possible to customize the style (font, color, text, and so on) of JSF default validation messages. Additionally, it is possible to modify the text of the default JSF validation messages. In the following sections, we will explain how to modify error message formatting and text.

. Customizing message styles c an be done via Cascading Style Sheets (CSS). This can be accomplished by using the <h:message> style or styleClass attributes. The style attribute is used when we want to declare the CSS style inline.

The styleClass attribute is used when we want to use a predefined style in a CSS stylesheet or inside a <style> tag in our page. The following markup illustrates using the styleClass attribute to alter the style of error messages. It is a modified version of the input page we saw in the previous section.

. < xml version="1.0" encod QR Code 2d barcode for None ing="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>Enter Customer Data</title> </h:head> <h:body> <h:outputStylesheet library="css" name="styles.

css" target="body"/> <h:form> <h:messages styleClass="errorMsg"></h:messages> <h:panelGrid columns="2" columnClasses="rightAlign,leftAlign"> <h:outputText value="First Name:"> [ 225 ]. Customizing message styles JavaServer Faces </h:outp Software QR utText> <h:inputText label="First Name" value="#{customer.firstName}" required="true" validator="#{alphaValidator.validateAlpha}"> <f:validateLength minimum="2" maximum="30"> </f:validateLength> </h:inputText> <h:outputText value="Last Name:"></h:outputText> <h:inputText label="Last Name" value="#{customer.

lastName}" required="true" validator="#{alphaValidator.validateAlpha}"> <f:validateLength minimum="2" maximum="30"> </f:validateLength> </h:inputText> <h:outputText value="Email:"> </h:outputText> <h:inputText label="Email" value="#{customer.email}"> <f:validator validatorId="emailValidator" /> </h:inputText> <h:panelGroup></h:panelGroup> <h:commandButton action="confirmation" value="Save"> </h:commandButton> </h:panelGrid> </h:form> </h:body> </html>.

As we can see, the only diff erence between this page and the previous one is the use of the styleClass attribute of the <h:messages> tag. Like we mentioned earlier, the value of the styleClass attribute must match the name of a CSS style defined in a cascading stylesheet our page has access to. In our case, we defined a CSS style for messages as follows:.

errorMsg { color: red; }.
Copyright © reffec.com . All rights reserved.